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);
}
}
Does
parentFile.makeCopy()
only copy the source spreadsheet as it was before the script ran, without edits?
Yes. Apps Script could wait to apply the changes after the script has finished. If you need to be certain that the changes were applied before the copy is made, use SpreadsheetApp.flush()
prior to making the copy.
Do I need a different method?
Maybe, if SpreadsheetApp.flush()
is not enough. An alternative could be to log the child spreadsheets ids / URLs somewhere, and use the SpreadsheetApp
methods open(File)
, openById(fileId)
or openByUrl(fileUrl)
to open the child spreadsheet. At that point, you could then use Range#setValue()
to update the corresponding cell on the child spreadsheet.
It's worth noting that file.makeCopy()
returns a File
object referring to the new file. To get its ID and the open the child spreadsheet, you could use something like the following:
var child = SpreadsheetApp.open(parentFile.makeCopy("new name","parent Folder"));
saveFileIdSomehow(child.getId());
// do something with the child spreadsheet
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.