简体   繁体   English

Google表格,在脚本保存公式中使用deleterows

[英]Google Sheet, using deleterows in script keeping formulas

I have a script that runs every 10 minutes and deletes 1 row on every iteration to keep always 144 rows for a 24 hours chart. 我有一个脚本,该脚本每10分钟运行一次,并在每次迭代中删除1行,以在24小时图表中始终保留144行。 It works pretty well for a sheet without formulas, but the other 3 sheets got formulas in them referring to the first one and when the script runs, they don't do what I would expect. 对于没有公式的工作表,它工作得很好,但是其他3个工作表中有引用第一个工作表的公式,并且在脚本运行时,它们没有达到我的期望。 Here's the script: 这是脚本:

  var tableData = ss.getSheetByName("TableData");
  var rowsToKeep = 149;   
  var totalRows = tableData.getLastRow();
  var numToDelete = totalRows - rowsToKeep;
      if (numToDelete > 0)
       {
         tableData.deleteRows(6, numToDelete);        
}

  var chartData1 = ss.getSheetByName("ChartData1-15");
  var rowsToKeep1 = 146;   
  var totalRows1 = chartData1.getLastRow();
  var numToDelete1 = totalRows1 - rowsToKeep1;
      if (numToDelete1 > 0)
       {
         chartData1.deleteRows(3, numToDelete1);          
}


  var chartData2 = ss.getSheetByName("ChartData10-20");
  var rowsToKeep2 = 146;   
  var totalRows2 = chartData2.getLastRow();
  var numToDelete2 = totalRows2 - rowsToKeep2;
      if (numToDelete2 > 0)
       {
         chartData2.deleteRows(3, numToDelete2);          
}


  var chartData3 = ss.getSheetByName("ChartData15-64");
  var rowsToKeep3 = 146;   
  var totalRows3 = chartData3.getLastRow();
  var numToDelete3 = totalRows1 - rowsToKeep3;
      if (numToDelete3 > 0)
       {
         chartData3.deleteRows(3, numToDelete3);         
}

Formulas repeated in rows and columns are: 在行和列中重复的公式是:

=IF(TableData!B$3>=$A$1, TableData!B5,"") =IF(TableData!B$3>=$A$1, TableData!B6,"") =IF(TableData!B$3>=$A$1, TableData!B7,"") = IF(TableData!B $ 3> = $ A $ 1,TableData!B5,“”)= IF(TableData!B $ 3> = $ A $ 1,TableData!B6,“”)= IF(TableData!B $ 3> = $ A $ 1,TableData!B7,“”)

and so on.... - for chartData1 等等...-对于chartData1

=IF(and(TableData!B$3>=$B$1, TableData!B$3<=$A$1), TableData!B5,"") =IF(and(TableData!B$3>=$B$1, TableData!B$3<=$A$1), TableData!B6,"") =IF(and(TableData!B$3>=$B$1, TableData!B$3<=$A$1), TableData!B7,"") = IF(and(TableData!B $ 3> = $ B $ 1,TableData!B $ 3 <= $ A $ 1),TableData!B5,“”)= IF(and((TableData!B $ 3> = $ B $ 1,TableData! B $ 3 <= $ A $ 1),TableData!B6,“”)= IF(and(TableData!B $ 3> = $ B $ 1,TableData!B $ 3 <= $ A $ 1),TableData!B7,“”)

and so on.... - for chartData2 依此类推......-针对chartData2

=IF(TableData!B$3<=$A$1, TableData!B5,"") =IF(TableData!B$3<=$A$1, TableData!B6,"") =IF(TableData!B$3<=$A$1, TableData!B7,"") = IF(TableData!B $ 3 <= $ A $ 1,TableData!B5,“”)= IF(TableData!B $ 3 <= $ A $ 1,TableData!B6,“”)= IF(TableData!B $ 3 <= $ A $ 1,TableData!B7,“”)

and so on... - for chartData3 依此类推...-适用于chartData3

What happens is: in chartData1 and 3 the rows change in =IF(TableData!B$3>=$A$1, #REF!,""), like missing the reference in TableData because it gets deleted; 发生的情况是:在chartData1和3中,行更改为= IF(TableData!B $ 3> = $ A $ 1,#REF !,“”),就像在TableData中丢失了引用一样,因为它被删除了; in chartData2 the rows below are translated above carrying the down formulas with them. 在chartData2中,下面的行在上面带有向下公式的情况下进行了转换。 Is there a way to solve my problem? 有办法解决我的问题吗? Maybe a script that could recopy again the formulas from the first row (the one not deleted)? 也许可以重新复制第一行公式(未删除的公式)的脚本?

Thank you 谢谢

Copy the values from the cells that the formulas refer to - either up, down, or whatever appropriate. 从公式引用的单元格中复制值-向上,向下或任何适当的值。 Then overwrite them with new data. 然后用新数据覆盖它们。 This will keep you from having to replace formulas, which would be more tedious. 这将使您不必替换公式,这将变得更加乏味。

暂无
暂无

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

相关问题 如何使用脚本在工作表中提取Google工作表公式? - How to extract Google sheet formulas in sheet with a script? 将值和公式复制到Google脚本中的其他工作表 - Copy values and formulas to a different sheet in google script 使用脚本将数据移动到另一个电子表格时,Google 工作表脚本会删除公式 - Google sheet script removes formulas when using a script to move data into another spreadsheet 谷歌工作表脚本将工作表复制到带有值而不是公式的新选项卡 - Google Sheet Script to copy a a sheet to a new tab with values and not formulas 是否可以在数组中使用带有 R1C1 公式的 Google Apps 脚本在 Google 表格中附加一行? - Is it possible to append a row in a Google Sheet using Google Apps Script with R1C1 formulas inside the array? Google表单/表格脚本清除公式,行中的条件格式 - Google Form/Sheet script clears formulas, conditional formatting in row 谷歌工作表脚本自动添加带有复制公式的新行 - Google sheet script auto add new rows with copied formulas 如何制作谷歌工作表脚本忽略带有公式的字段 - How to make google sheet script Ignore fields with formulas 如何在不显示“目标”选项卡/工作表的情况下使用 copyTo(),将用户保持在原处(使用 Google Apps 脚本)? - How to use copyTo() without showing "destination" tab/sheet, keeping the user where he is (using Google Apps Script)? 谷歌应用脚本:打开谷歌表格,等待所有公式处理完毕,关闭表格 - Google App Script: Open Google Sheet, wait for all formulas to process, close sheet
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM