Auto fill a column with formulas when new rows are added

亡梦爱人 提交于 2020-01-03 02:43:48

问题


I have an automation workflow (Zapier) which adds a new row to a Google Sheets whenever a new card is added in Trello. We use it to track statistics for departments etc. I have some extra fields which transform a date field into things like weeknumber, day, month, year... simple stuff.

I need to write a script which looks out for new rows entered into the spreadsheet and then auto-fills the other columns with preset formulas.

Example layout:

Columns populated via the automation:

a,b,d,e

Columns to populate via the script.

f,g,h,i,j,k

I've seen a few scripts that are similar but don't take a formula from the row above as the content to autofill.

Note: The formulas for each column are available in the row above which has been added manually by me (for now). So, in theory, the script could reference the cell above for the correct formula to use.

Note 2: I cannot use the ARRAYFORMULA method because Zapier will see the row as having content and will skip to the next empty row.


回答1:


I think this will do it.

function fillInFGHIJK()
{
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  for(var i=0;i<vA.length;i++)
  {
    if(!vA[i][5] && !vA[i][6] && !vA[i][7] && !vA[i][8] && !vA[i][9] && !vA[i][10])
    {
      sh.getRange(i,6,1,6).copyTo(sh.getRange(i+1,6,1,6));//According to Michelle this will works better because the cell references change appropriately
    }
  }
}


来源:https://stackoverflow.com/questions/45738468/auto-fill-a-column-with-formulas-when-new-rows-are-added

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