Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Google apps script 在语句为true时清除最后一行_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script 在语句为true时清除最后一行

Google apps script 在语句为true时清除最后一行,google-apps-script,google-sheets,Google Apps Script,Google Sheets,如果语句为true,如何循环函数以清除最后一行 例如: 在第一列中,我在不同的行中有日期或字符串'BugFix'。我需要清除每一个“错误修正”,如果它是在最后一行 Column A ------ 1 BugFix 2 some Date 3 BugFix 4 some Date 5 BugFix 6 BugFix 在本例中,我需要清除两行:5和6 清除最后一行(第6行),因为该值为BugFix 检查新的最后一行(第5行)中是否存在错误修复 很明显,该行再次检查,但什么也不做,

如果语句为true,如何循环函数以清除最后一行

例如: 在第一列中,我在不同的行中有日期或字符串'BugFix'。我需要清除每一个“错误修正”,如果它是在最后一行

Column A
  ------
1  BugFix
2  some Date
3  BugFix
4  some Date
5  BugFix
6  BugFix
在本例中,我需要清除两行:5和6

  • 清除最后一行(第6行),因为该值为BugFix
  • 检查新的最后一行(第5行)中是否存在错误修复
  • 很明显,该行再次检查,但什么也不做,因为在新的最后一行(4)中是某个日期
我的代码只清除第6行。请帮忙

function clearLogs(){
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('logs');
  var range = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn())
  var data = range.getValues();
  var lastRow = sheet.getRange(sheet.getLastRow(), 1).getValue(); 

  if (lastRow == 'BugFix') {
    sheet.getRange(sheet.getLastRow(),1).clear();    
  }
}

第一个和你的例子一样,它只是清除了最后一行

function clearLogs(){
  var sheet = SpreadsheetApp.getActive().getSheetByName('logs');
  var range = sheet.getRange(2, 1, sheet.getLastRow()-1, sheet.getLastColumn());//Third parameter is the number of rows not the last row
  var data = range.getValues();
  var lastRow = sheet.getRange(sheet.getLastRow(), 1).getValue();//I assumed the same condition 
  if (lastRow=='BugFix') {
    sheet.getRange(sheet.getLastRow(),1,1,sheet.getLastColumn()).clear();//This clears the entire row    
  }
}
这一行删除第一列中带有“BugFix”的所有行:

function clearLogs1() {
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('Sheet1');
  var rg=sh.getRange(2,1,sh.getLastRow()-1,sh.getLastColumn());
  var data=rg.getValues();
  var d=0;
  data.forEach(function(r,i){if(r[0]=='BugFix'){sh.deleteRow(i+2-d++);}});
}

你需要做的是从“自下而上”分析你的价值观

此代码可为您执行以下操作:

function clearLastBugFixes() {
  var ss = SpreadsheetApp.getActiveSheet();
  var rowAValues = ss.getDataRange().getValues().map(function(row) {return row[0];}); //The Map functions filters the values only to the ones we want (Column A).
  Logger.log(rowAValues);

  var counter = 0;
  while (rowAValues.pop()=="BugFix") {
    counter++;
  }
  if (counter>0) {
    var deletionRange = ss.getRange(rowAValues.length+2, 1, counter+1, 1); //Gets the range with lines that need to be deleted.
    deletionRange.clear();
  }
}
它所做的是:

  • 从工作表中获取数据
  • 将这些值筛选到要分析的列
  • 计算需要从底部删除的单元格数:
  • 最后一个值,并将其与“BugFix”进行比较
  • 如果这是真的,请向计数器添加一个数字
  • 当这不再为真时,停止计数。(因此,
    while
    循环代替了
    for
  • 获取具有正确坐标的
    范围
    (要基于此数据清除多个列,只需扩展范围以获得正确的列
  • 处理多种情况 第一个选项:For循环 您可以为执行相同类型功能的
    for
    循环更改
    while
    循环:

    变成:

      var counter;
      var rowValue;
      for(counter=0,rowValue=rowAValues.pop(); rowValue=="BugFix" ||  rowValue=="SomethingElse"; counter++) {}
    
    
    第二种选择:另一种功能 为了使您的主函数不受条件限制,并允许更容易的扩展,您还可以将筛选委托给另一个函数

    创建一个新函数,如下所示:

    function filterValue(rowValue) {
      return rowValue=="BugFix" ||  rowValue=="SomethingElse";
    }
    
    然后改变:

    致:


    如果我想在最后一行中再添加一个条件进行检查,即如果最后一行是“BugFix”或“something_Other”,如何添加它?谢谢,一切正常-我使用的第二个解决方案更简单
    function filterValue(rowValue) {
      return rowValue=="BugFix" ||  rowValue=="SomethingElse";
    }
    
      while (rowAValues.pop()=="BugFix") {
    
      while (filterValue(rowAValues.pop())) {