Google脚本触发器仅在星期一/星期三/星期五运行

时间:2018-11-19 22:56:52

标签: javascript google-apps-script google-sheets

我正在运行一个 SendEmail 脚本,其中包含3个触发器,这些触发器将在星期一,星期三和星期五发送出去。

我在电子表格上有10张纸(每张纸都包含一个 SentEmail 脚本,每张都需要在当天发送出去,但我只有20个触发器限制)

这是代码:

function sendEmail() {
  var s = SpreadsheetApp.getActive().getSheetByName('BCX');
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var range = ss.getActiveSheet().getDataRange();
  var range = s.getRange('B5:Q20');
  var row = ss.getSheetByName('BCX').getRange("J1").getValue();
  var to = "info@google.com";
  var body = '';
  var htmlTable = SheetConverter2.convertRange2html(range);
var body = "Hi Team!"
     + htmlTable
     + "<br/><br/><b><i>**This is an automated email**</i></b><br/><br/>Any question please let me know.<br/><br/>Regards,<br/><br/>";
  var subject = "Google | Report " + row;

  MailApp.sendEmail(to, subject, body, {htmlBody: body});
};

但是,如果我使用类似以下脚本的内容,它将每周创建3个触发器,直到达到20个触发器(触发限制)。

function createTriggers() {
   var days = [ScriptApp.WeekDay.MONDAY, 
               ScriptApp.WeekDay.WEDNESDAY,                                            
               ScriptApp.WeekDay.FRIDAY];
   for (var i=0; i<days.length; i++) {
      ScriptApp.newTrigger("sendEmail")
               .timeBased().onWeekDay(days[i])
               .atHour(7).create();
   }
};

1 个答案:

答案 0 :(得分:0)

该问题的一种解决方案是将各种脚本组合成一个脚本,可以触发该脚本在星期一,星期三和星期五运行。

在脚本中,处理顺序为:
1)遍历Google云端硬盘给定文件夹/子文件夹中的电子表格。 -这提供了唯一的电子表格ID。
2)对于每个电子表格,获取ID并使用openById(ID)打开电子表格。
3)获取电子表格的表格
4)对于每张纸,使用原始代码构建并发送电子邮件。
5)冲洗并重复下一张纸和下一张电子表格。

以下 unested 代码结合了对特定文件夹和子文件夹中每个电子表格的搜索,打开电子表格并获取工作表,然后遍历每个工作表。发问者仅需添加Google云端硬盘文件夹的名称即可启动搜索,并将其自己的代码放在所示的两个位置。

function 53383834() {
    /* Adapted from Code written by @hubgit https://gist.github.com/hubgit/3755293
    Updated since DocsList is deprecated  https://ctrlq.org/code/19854-list-files-in-google-drive-folder
    */


    // List all files and sub-folders in a single folder on Google Drive
    // declare the folder name
    var foldername = 'XXXXXXXXXXXXXXXXXX'; // enter the folder name

    // declare this sheet
    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var sheet = ss.getActivesheet();

    // getFoldersByName = Gets a collection of all folders in the user's Drive that have the given name.
    // folders is a "Folder Iterator" but there is only one unique folder name called, so it has only one value (next)
    var folders = DriveApp.getFoldersByName(foldername);
    var foldersnext = folders.next();

    // list files in this folder
    var myfiles = foldersnext.getFiles();

    // spreadsheets have a unique MIME-Type = application/vnd.google-apps.spreadsheet
    var searchTerm = 'spreadsheet';

    // loop through files in this folder
    while (myfiles.hasNext()) {
        var myfile = myfiles.next();
        var fname = myfile.getName();
        var fid = myfile.getId();

        // get the MIME-Type and test whether the file is a spreadsheet
        var ftype = myfile.getMimeType();
        var indexOfFirst = ftype.indexOf(searchTerm);
        if (indexOfFirst != -1) {
            var ssid = fid;
            // open the spreadsheet
            var sso = SpreadsheetApp.openById(ssid);
            // get the sheets
            var sheets = sso.getSheets();
            var sheetlen = sheets.length;
            for (var i = 0; i < sheetlen; i++) {
                // get the sheets, one by one
                var thissheet = sso.getSheets()[i];

                <<
                insert questioners code here >>

            }

        }

    }


    // Now get the subfolder
    // subfolders is a Folder Iterator
    var subfolders = foldersnext.getFolders();


    // now start a loop on the SubFolder list
    while (subfolders.hasNext()) {
        var subfolderdata = [];
        var mysubfolders = subfolders.next();
        var mysubfolder = mysubfolders.getName();

        // Get the files
        var mysubfiles = mysubfolders.getFiles();

        // now start a loop on the files in the subfolder
        while (mysubfiles.hasNext()) {

            var smyfile = mysubfiles.next();
            var sfname = smyfile.getName();
            var sfid = smyfile.getId();
            var sftype = smyfile.getMimeType();
            var indexOffolder = sftype.indexOf(searchTerm);
            if (indexOffolder != -1) {
                var ssid = sfid;
                // open the spreadsheet
                var sso = SpreadsheetApp.openById(ssid);
                // get the sheets
                var sheets = sso.getSheets();
                var sheetlen = sheets.length;
                for (var i = 0; i < sheetlen; i++) {
                    // get the sheets, one by one
                    var thissheet = sso.getSheets()[i];

                    <<
                    insert questioners code here >>

                }

            }

        }

    }

}