Copy formula to range. Google Spreadsheet

google-apps-script, google-sheets

Solution

Take a look at the getFormulas and getFormulasR1C1 methods of the `Range` class. You'll also need to use the corresponding `setFormulas` or `setFormulasR1C1`.

[edit: to answer the comments]

If in the multi-cell range you're copying mixed values and formulas. You'll have to break up the calls into multiple sets, for either value or formula, as a single cell can only have one (or parse the plain values into simple formulas, e.g. ="value").

To have your formulas "following" the reference, use the `set/getFormulasR1C1` notation instead of the simpler `set/getFormulas`.

Problem

I currently have this script : ``` function addConsultant() { var sheet_staffing = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Staffing"); var sheet_parametres = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Parametres"); var range_tabConsultant_default = sheet_parametres.getRange(26,1,6,64); var row_position_newTab = sheet_parametres.getRange(1,1).getValue(); range_tabConsultant_default.copyFormatToRange(sheet_staffing, 1, 36, row_position_newTab, row_position_newTab + 6); range_tabConsultant_default.copyValuesToRange(sheet_staffing, 1, 36, row_position_newTab, row_position_newTab + 6); sheet_parametres.getRange(1,1).setValue(row_position_newTab+6); } ``` This is like a default tab, their is one tab per person, but I dont know how to copy formulas from def tab to tab created by script. Can someone help me with this?

Original source