Add if statement script to new rows of spreadsheet

雨燕双飞 提交于 2020-07-23 07:13:10

问题


I'm struggling to append an if-statement down a column because my data table adds rows each day. I need the if-statement to be added to the new rows that join the spreadsheet every 24 hours.

Within the table, column C contains a date that my if-statement is referencing.

I'm trying to add the if-statement in a column off to the right (in column Q) that will tell me if the date in column C is before or after today's date. The problem is I'm not sure how to add the function for the incremental rows that are appended to the table each day. Below is what I have:

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);
}

You may be able to tell I'm totally stuck because above has several errors. How can I have my if statement added to the active rows down column Q on a daily basis?


回答1:


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)
}



回答2:


Why not use an array formula in your top row?

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

To limit the range that the array formula is applied to you can use indirect()

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


来源:https://stackoverflow.com/questions/61312185/add-if-statement-script-to-new-rows-of-spreadsheet

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!