【问题标题】:Create daughter Google spreadsheet with edits using GAS使用 GAS 创建带有编辑的子 Google 电子表格
【发布时间】:2018-07-19 06:32:09
【问题描述】:

我需要有关 Google Apps 脚本的帮助,该脚本使用模板报告文件,并通过在引用的单元格中设置 ID 号来生成个性化报告 - 这会更改动态更新的报告。

脚本正常工作,因为它为数据数组中的每个 ID 创建一个新文件。但是,所有子文件看起来都与父文件(模板)相同。它们不反映基于setValue() 对单元格C3 所做的更改。但是,在运行脚本后,模板文件确实显示 setValue 在脚本运行时对 C3 进行了更改。

parentFile.makeCopy() 是否只复制脚本运行前的源电子表格而不进行编辑?我需要其他方法吗?

另外,我将如何更改这些子文件的权限?我是否必须每次在新女儿和源电子表格之间来回更改我的活动电子表格?

感谢您的帮助!

我在下面包含了我的脚本副本:

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);
  }
}

【问题讨论】:

标签: google-apps-script google-sheets google-drive-api


【解决方案1】:

parentFile.makeCopy() 是否只复制脚本运行前的源电子表格而不进行编辑?

是的。 Apps 脚本可能会在脚本完成后等待应用更改。如果您需要确定在复制之前应用了更改,请在复制之前使用SpreadsheetApp.flush()

我需要不同的方法吗?

也许,如果 SpreadsheetApp.flush() 还不够的话。另一种方法是在某处记录子电子表格的 ID/URL,并使用 SpreadsheetApp 方法 open(File)openById(fileId)openByUrl(fileUrl) 打开子电子表格。此时,您可以使用Range#setValue() 更新子电子表格上的相应单元格。

值得注意的是file.makeCopy() 返回一个引用新文件的File 对象。要获取其 ID 并打开子电子表格,您可以使用以下内容:

var child = SpreadsheetApp.open(parentFile.makeCopy("new name","parent Folder"));
saveFileIdSomehow(child.getId());
// do something with the child spreadsheet

【讨论】:

  • @tehhowch 而不是你的评论我读懂了你的想法:)(当你发表评论时我正在编辑我的答案)
  • 我相当肯定这些更改会“在合理的情况下”应用。模板实例化的问题在于 OP 可能有相当多的公式根据给定的更改单元格进行操作,而 Google 可以检测到这一点。对于高延迟/响应时间的操作,我觉得这是批量写入优化最多的地方。
  • @tehhowch AFAIK 如果未使用 SpreadsheetApp.flush(),我们无法确定在脚本完成之前应用了更改。
  • @Rubén 我真的很喜欢你的想法,但你能帮我理解如何将子电子表格 ID 设置为变量,以便使用 SpreadsheetApp.openById(id) 打开子电子表格你建议? makeCopy() 方法不返回 ID。非常感谢您的帮助!
  • @Joshua 请注意,如果您有一个您知道属于MimeType.GOOGLE_SHEETSDriveApp#File,您可以直接调用SpreadsheetApp.open
猜你喜欢
  • 2020-07-17
  • 2013-08-25
  • 2020-12-21
  • 1970-01-01
  • 2022-11-17
  • 1970-01-01
  • 2014-12-06
  • 2023-01-19
  • 1970-01-01
相关资源
最近更新 更多