Jake
Jake

Reputation: 69

Auto fill a column with formulas when new rows are added

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.

Upvotes: 3

Views: 2498

Answers (1)

Cooper
Cooper

Reputation: 64062

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

Upvotes: 3

Related Questions