3

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?

3 Answers 3

4

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.

3
  • Its working, but not as I expected, for exemple, I want D40 =sum(D41:D45) but the next tab I'll add on my sheet will need the formula D46=(D47:D51). No way to do that?
    – ElPoney
    Jun 11, 2012 at 13:50
  • And if I set this : range_newTab.setFormulas(Formulas_tab); before my CopyValueToRange(), it dont work, and if I set it after, CopyValueToRange() dont work.
    – ElPoney
    Jun 11, 2012 at 13:52
  • Ok, after all its working, but if I use a copyValueToRange, I've to use many setFormula (because setFormulas dont work). Thanks for your help !
    – ElPoney
    Jun 13, 2012 at 7:58
0

What if the script used the Current sheet the user was looking at with this function ? GetActiveSheet

1
  • I dont think it will work, because if the user launch the script from the wrong sheet, it will make a tab on the sheet and overwrite information. Its why Im using a cell with the position of the row that is after the last tab.
    – ElPoney
    Jun 11, 2012 at 13:12
0

Here is the sample how to copy formula in A1 to A2:A range with relative references:

var sheet = SpreadsheetApp.getActiveSpreadsheet();
sheet.getRange("A2:A").setFormulaR1C1(sheet.getRange("A1").getFormulaR1C1());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.