Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/421.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 在导入后隐藏行_Javascript_Google Apps Script_Google Sheets - Fatal编程技术网

Javascript 在导入后隐藏行

Javascript 在导入后隐藏行,javascript,google-apps-script,google-sheets,Javascript,Google Apps Script,Google Sheets,我有2个文件,从第一个文件导入3列到第二个文件,如果导入的第3列中的值为true,我要做的是隐藏一行。 我发现在线脚本非常有用: //GLOBALS // Sheet the data is on. var SHEET = "D&D Miniatures"; // The value that will cause the row to hide. var VALUE = "TRUE"; // The column we will be usin

我有2个文件,从第一个文件导入3列到第二个文件,如果导入的第3列中的值为true,我要做的是隐藏一行。 我发现在线脚本非常有用:

//GLOBALS
// Sheet the data is on.
var SHEET = "D&D Miniatures";
// The value that will cause the row to hide. 
var VALUE = "TRUE";
// The column we will be using 
var COLUMN_NUMBER = 3
 
function onEdit(e) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var activeSheet = ss.getActiveSheet();
  
  //Ensure on correct sheet.
  if(SHEET == activeSheet.getName()){
    var cell = ss.getActiveCell()
    var cellValue = cell.getValue();
    
    //Ensure we are looking at the correct column.
    if(cell.getColumn() == COLUMN_NUMBER){
      //If the cell matched the value we require,hide the row. 
      if(cellValue == VALUE){
        activeSheet.hideRow(cell);
      };
    };
  };
}

问题是,仅当您手动修改单元格时,它才起作用。是否可以使taht代码适应导入?

当您打开名为“隐藏行”的文件时,此代码将添加一个新菜单。导入后只需选择菜单项“Hide Rows with Column 3=True”,它将遍历每一行并隐藏Column 3=True被视为“truthy”的任何行。如果它是“True”或“True”或“True”或“1”,则第3列将被视为True

添加了OneEdit()


请提供一个工作表示例、您拥有的数据以及您想要的结果。难道您不能简单地不导入第3列中为True的行吗?该代码非常有用,但我的目标是构建无点击的内容,导入数据时,如果第3列为True,它会自动隐藏该行。我添加了一个onEdit()函数。

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Hide Rows')
  .addItem('Hide Rows with Column 3 = True','hide')
  .addItem('Unhide all rows','unhide')
  .addToUi();
};

function onEdit(e) {
  const sheet = e.source.getActiveSheet();
    if(sheet.getName() != "D&D Miniatures"){
      return;
    }else{
      hide();
    };
};

function hide(){
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('D&D Miniatures');
  const data = sheet.getRange(1,1,sheet.getLastRow(),sheet.getMaxColumns()).getValues();
  
  data.forEach((datum,index) => {
    datum[2]==true ? sheet.hideRows(index+1,1) : false;
  });
}

function unhide() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = ss.getSheetByName('D&D Miniatures');
  sheet.unhideRow(sheet.getRange("A:A"));
}