I am trying to write a script so that when a new row is added to our google sheet, it will copy a template spreadsheet and paste the link into the corresponding column. The sheet is updated automatically when a new person signs up.
The script below can copy the template sheet, rename it based on the adjacent text in row A and then and paste the link in column 20.
Unfortunately, every time it runs it creates new sheets for all rows in the sheet, instead of just finding the latest row and creating 1 new sheet.
Also, I want to set the share permissions for the new share the spreadsheet to a certain group, rather than anyone with the link.
////function copyTo() {
// var tempsheet = SpreadsheetApp.openById("1q....");
//var mastersheet = tempsheet.getSheetByName("Setting Sheet");
//var lrow = mastersheet.getLastRow();
//for (i= 2; i<lrow; i++ ) {
//let names = mastersheet.getRange(i,1).getValues();
//sheet = tempsheet.copy(names);
//}
//}
//function createSpreadsheet(){
//var tempsheet = '1....';
//var sheetName = "MASTER"
//SpreadsheetApp.openById(tempsheet).copy(sheetName);
//for (r=2; r<lrow; r++) {
//let name = ss.getRange(r,1).getValue();
function cloneGoogleSheet(fileName) {
var destFolder = DriveApp.getFolderById("19Q....");
var newFile = DriveApp.getFileById("1vvgD....").makeCopy(fileName, destFolder);
return newFile.getUrl();
}
function createSheets() {
var workBook = SpreadsheetApp.getActiveSpreadsheet();
var sheet = workBook.getSheetByName("Sheet1")
var data = sheet.getRange("A2:A" + sheet.getLastRow()).getValues()
data.forEach((row, index) => {
sheet.getRange(index + 2, 20).setValue(cloneGoogleSheet(row[0]))
})
}




data.forEach()will create a new file for every row of column A containing data. So if you add a new row of data theforEachwill create a new file for the old data as well as the new row data. You probable need to include a check if the url exists skip the copy process.