【问题标题】:How to automatically create triggers in a new spreadsheet?如何在新电子表格中自动创建触发器?
【发布时间】:2021-04-15 22:13:33
【问题描述】:

我想做的是:

  • 使用 Google Apps 脚本创建新电子表格
  • 为这个新创建的电子表格创建触发器

这是我的代码:

function createfile() {
    var timezone = Session.getScriptTimeZone();
    var tempStartDate = new Date();
    tempStartDate.setDate(tempStartDate.getDate() + 21); 
    var startDate = Utilities.formatDate(tempStartDate, timezone, 'yyMMdd');

    var tempEndDate= new Date();
    tempEndDate.setDate(tempEndDate.getDate()+26);
    var endDate= Utilities.formatDate(tempEndDate, timezone, 'yyMMdd');

    var ss = SpreadsheetApp.openById('123123123123123C3Znd64gFUvGnpCLTbr54s');

    //Make a copy of the template file
    var documentId = DriveApp.getFileById('123123123123123C3Znd64gFUvGnpCLTbr54s').makeCopy().getId();

    //Rename the copied file
    var file = DriveApp.getFileById(documentId).setName('testaaaaaaaaaaaaaa' + startDate+'-'+endDate );
    ScriptApp.newTrigger('monday')
    .timeBased()
    .everyHours(6)
    .create();
}

【问题讨论】:

  • 您是否遇到任何错误或者您能否提供一些日志?

标签: google-apps-script google-sheets


【解决方案1】:

没有办法在工作表上做到这一点

触发器是有限的,您不能将基于时间的触发器附加到电子表格中。您可以将其他触发器附加到其他电子表格,例如 onEdit,但不能附加时钟触发器。

基于时间的触发器附加到 脚本

此外,脚本只能为自身创建基于时间的触发器,而不能为其他脚本创建。

您可能认为可以从另一个脚本创建一个新脚本,并且有一个 API,即 Apps Script API。但是,API不允许允许您使用它创建触发器。

因此,遗憾的是,无法创建项目的副本,然后完全从不同的脚本向其添加触发器。总会涉及到一些手动操作。您可以复制它,但要初始化触发器,您需要从副本运行该函数。

参考文献

【讨论】:

    猜你喜欢
    • 2014-02-02
    • 1970-01-01
    • 1970-01-01
    • 2015-07-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多