简体   繁体   English

使用 Google Apps 脚本隐藏行

[英]Hiding Rows using Google Apps Script

I'm looking to create almost a drop-down feature in Google Sheets.我希望在 Google 表格中创建几乎一个下拉功能。 I was hoping to have arrows set that could potentially collapse rows when clicked.我希望设置箭头,单击时可能会折叠行。

It would be amazing if I could get it so each main "title" row has an arrow, that would cause the rows to either hide/unhide.如果我能得到它,那就太棒了,所以每个主要的“标题”行都有一个箭头,这会导致行隐藏/取消隐藏。

Since only the titles would be written in column A, I was thinking it might be possibly for a script to recognize the blank cells in between two filled ones?由于只有标题会写在 A 列中,我在想脚本可能会识别两个填充单元格之间的空白单元格?

So far I only have this, which is basically useless:目前我只有这个,基本没用:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheets()[0];
// Hides the first three rows
sheet.hideRows(1, 3);

In a perfect world, the spreadsheet would look something like this, with the functionality to collapse the rows with the click of the arrow.在一个完美的世界中,电子表格看起来像这样,具有通过单击箭头折叠行的功能。

https://docs.google.com/spreadsheets/d/1-oGawFoPXjdHvadxXsSFxuZWWdOxN9ZgbvS10jelWZY/edit?usp=sharing https://docs.google.com/spreadsheets/d/1-oGawFoPXjdHvadxXsSFxuZWWdOxN9ZgbvS10jelWZY/edit?usp=sharing

Not sure if this is possible, but if there is any hope/ideas I'd love to hear them.不确定这是否可能,但如果有任何希望/想法,我很想听听。

Instead, have you considered using the group row facility?相反,您是否考虑过使用组行功能? You can just group and ungroup the rows to show/hide them.您可以对行进行分组和取消分组以显示/隐藏它们。

  • Just select all the rows that you want to group,只需 select 您要分组的所有行,
  • right click select Group rows.右键单击 select 组行。

I have added the same in the spreadsheet that you have shared.我在您共享的电子表格中添加了相同的内容。

在此处输入图像描述

function onEdit(e) {
  const sh=e.range.getSheet();
  const shts=['Sheet13'];
  if(shts.indexOf(sh.getName())!=-1 && e.range.columnStart==1 && e.value=="TRUE") {
    e.range.setValue("FALSE");
    sh.hideRows(Number(e.range.rowStart));
    e.source.toast('Row ' + e.range.rowStart + ' has been hidden.');
  }
}

Animation: Animation:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM