Consider I have a masterSheet with names in column A and a link in colum B, starting in row 2. I'd like that for every row added in the masterSheet, it automatically :
- Creates new sheet that has the name of the person in column A
- Sets the values of A and B in this newly created sheet
- Sets my IMPORTRANGE formula in C
- Sets my CONCATENATE formula in J2:J50
The points 1. and 2. are working well. Points 3. and 4. don't work at all and I get an error message like this one : TypeError: sheetNames.getRange is not a function
I can't figure out what is the problem about this but I'm pretty sure it comes from the place I put them in the loop or something.
Can someone help me on this one ?
function createNewSheets() {
// 1. Retrieve the current sheet names.
var dataSheet = SpreadsheetApp.getActiveSpreadsheet();
var sheetNames = dataSheet.getSheets().map(s => s.getSheetName());
// 2. Retrieve the values from "mastersheet" sheet.
var masterSheet = dataSheet.getSheetByName('Liste de nageurs');
var values = masterSheet.getRange('A2:B' + masterSheet.getLastRow()).getValues();
// Imports table from link
var swimmerTable = '=IMPORTHTML(B1;"table";4)'
// Concatenates event with pool size
var fullName = 'CONCATENATE(C2;" - ";D2)'
// 3. Using the retrieved sheet names and values, the new sheets are inserted and the values are put.
values.forEach(r => {
if (!sheetNames.includes(r[0])) {
dataSheet.insertSheet(r[0]).appendRow(r);
sheetNames.push(r[0]);
// Sets the formula in the 3rd column:
sheetNames.getRange(1, 3).setFormula(swimmerTable)
// Sets the formula2 and iterates it from row 2 to row 50
sheetNames.getRange("J2:J50").setFormula(fullName)
}
});
}