I need help with a Google Apps Script that uses a template reporting file, and spawns individualized reports by setting an ID number into a referenced cell - which changes the dynamically updating report.
The script works properly, in that it creates a new file for each ID in the data array. However, all of the daughter files look identical to the parent (template). They do not reflect the changes to cell C3 based on the setValue()
. However, after running the script, the template file does show that the setValue
was making changes to C3 as the script ran.
Does parentFile.makeCopy()
only copy the source spreadsheet as it was before the script ran, without edits? Do I need a different method?
Also, how would I go about changing the permissions of these daughter files? Would I have to change my active spreadsheet back and forth each time between new daughters and the source spreadsheet?
Thanks for any help!
I've included a copy of my script below:
function SpawnReports() {
// gets the current Google Sheet file
var parentFile = DriveApp.getFileById(SpreadsheetApp.getActiveSpreadsheet().getId())
var ss = SpreadsheetApp.getActiveSpreadsheet();
// the spreadsheet must have a sheet named ReportList with
// Column A = IDs and Column B = emails without headings on columns
var sheet = ss.getSheetByName("ReportList");
var data = sheet.getDataRange().getValues();
// determines timezone
var timeZone = Session.getScriptTimeZone();
// generates the timestamp and stores in variable
// formattedDate as year-month-date hour-minute-second
var formattedDate = Utilities.formatDate(new Date(), timeZone , "yyyy-MM-dd' 'HH:mm:ss");
// gets the destination folder using parent spreadsheet location (folder)
var parentFolder = parentFile.getParents();
var folder = parentFolder.next();
var folderId = folder.getId();
var destination = DriveApp.getFolderById(folderId);
// loops through IDs
for (var i = 0; i < data.length; i++) {
Logger.log('ID: ' + data[i][0]);
Logger.log('EMAIL: ' + data[i][1]);
// Write ID to daughter spreadsheet reference cell
sheet.getRange(1, 3).setValue(data[i][0]);
// gets the name of the original file and appends the ID and
// the timestamp stored in formattedDate
var name = SpreadsheetApp.getActiveSpreadsheet().getName() + " - " + data[i][0] + " Report " + formattedDate;
// makes copy of "parentFile" with "name" at the "destination"
parentFile.makeCopy(name, destination);
}
}