将if语句脚本添加到电子表格的新行

时间:2020-04-19 22:00:45

标签: javascript google-apps-script google-sheets

我正在努力在列的后面附加if语句,因为我的数据表每天都会添加行。我需要将if语句每24小时添加到加入电子表格的新行中。

在表中,列C包含我的if语句引用的日期。

我正在尝试在右侧(在Q列中)的一栏中添加if语句,以告诉我C列中的日期是在今天的日期之前还是之后。问题是我不确定如何为每天添加到表的增量行添加功能。以下是我所拥有的:

function AddFormula() {
  var spreadsheet = SpreadsheetApp.getActive();
  var column = spreadsheet.getRange('Q:Q').activate();
  var lastrow = 
  spreadsheet.getCurrentCell().setFormula('=if($C2<(today()),1,0)');
  spreadsheet.getActiveRange().autoFillToNeighbor(SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
}

您可能可以告诉我我完全被卡住了,因为上面有几个错误。每天如何将if语句添加到Q列下的活动行中?

2 个答案:

答案 0 :(得分:1)

为什么不在顶行中使用数组公式?

=ArrayFormula(if(C2:C < TODAY(),1,0))

要限制应用数组公式的范围,可以使用indirect()

=ArrayFormula(if(indirect("C2:C"&counta(C2:C)+1,1) < TODAY(),1,0))

答案 1 :(得分:0)

function AddFormula() {
  const ss=SpreadsheetApp.getActive();
  const sh=ss.getActiveSheet();
  const srg=sh.getRange(1,17).setFormula('=if($C2<(today()),1,0)');
  const drg=sh.getRange(1,17,sh.getLastRow(),1);
  srg.autoFill(drg,SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES)
}