我在一家物业管理公司工作,该公司既管理业主的财产,又将其出租给客人。我们希望有一种方法来认识我们的每个主人,并为他们的到来做更好的准备。考虑到这一目标,我创建了一个名为“向我展示你认识我”的项目。该项目由我们管理的每个所有者的单个Google文档和一个Google表格组成。电子表格上有一个包含所有即将到来的工作表和一个包含文档的所有ID及其相应单位编号的工作表。该功能旨在查看到达的货物,确定是否有船东('o')明天到达,如果是,请将其文档附加到电子邮件中并发送出去。我希望它每天执行一次。当我手动运行它时,它会按预期工作,但每次尝试触发它时都会超时。有什么方法可以使我的代码更快?
`function sendEmail() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var arrivals = ss.getSheetByName('Arrivals');
var ids = ss.getSheetByName('IDs');
var lastrow = arrivals.getLastRow(); // find the last row
var datarange = arrivals.getRange(2, 1, lastrow, 5).getDisplayValues();
var list = []; // create a blank list to store the attachment IDs
for (var i in datarange) {
var rowstart = 2;
var row = datarange[i]; // get the corresponding row
var difdate = row[3]; // fetch the days to arrival
var unitnum = row[1]; // fetch the unit number
var type = row[4]; // fetch the arrival type
if (difdate == 1 && type == 'o'){ // if an owner is arriving tomorrow
do {
rowstart++; }
while (unitnum != ids.getRange(rowstart, 1).getValue());
if (unitnum == ids.getRange(rowstart, 1).getValue()) {
list.push(DriveApp.getFileById(ids.getRange(rowstart,2).getValue()));
}
}
}
if (list.length > 0) { // if there's at least one thing on the list
MailApp.sendEmail('email@email.com', 'Show Me You Know Me', 'Read
about the owners arriving tomorrow!', {attachments: list});`
这是我的执行成绩单:
[19-05-15 10:40:31:406 MDT] Starting execution
[19-05-15 10:40:31:416 MDT] SpreadsheetApp.getActiveSpreadsheet() [0 seconds]
[19-05-15 10:40:31:548 MDT] Spreadsheet.getSheetByName([Arrivals]) [0.131 seconds]
[19-05-15 10:40:31:550 MDT] Spreadsheet.getSheetByName([IDs]) [0.001 seconds]
[19-05-15 10:40:31:712 MDT] Sheet.getLastRow() [0.161 seconds]
[19-05-15 10:40:31:714 MDT] Sheet.getRange([2, 1, 5, 5]) [0.001 seconds]
[19-05-15 10:40:31:850 MDT] Range.getDisplayValues() [0.135 seconds]
[19-05-15 10:40:31:852 MDT] Sheet.getRange([3, 1]) [0.001 seconds]
[19-05-15 10:40:31:968 MDT] Range.getValue() [0.115 seconds]
[19-05-15 10:40:31:974 MDT] Sheet.getRange([4, 1]) [0.001 seconds]
[19-05-15 10:40:31:975 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:976 MDT] Sheet.getRange([5, 1]) [0 seconds]
[19-05-15 10:40:31:976 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:977 MDT] Sheet.getRange([6, 1]) [0 seconds]
[19-05-15 10:40:31:977 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:978 MDT] Sheet.getRange([7, 1]) [0 seconds]
[19-05-15 10:40:31:978 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:979 MDT] Sheet.getRange([8, 1]) [0 seconds]
[19-05-15 10:40:31:979 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:979 MDT] Sheet.getRange([9, 1]) [0 seconds]
[19-05-15 10:40:31:980 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:980 MDT] Sheet.getRange([10, 1]) [0 seconds]
[19-05-15 10:40:31:980 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:981 MDT] Sheet.getRange([11, 1]) [0 seconds]
[19-05-15 10:40:31:981 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:982 MDT] Sheet.getRange([12, 1]) [0 seconds]
[19-05-15 10:40:31:982 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:983 MDT] Sheet.getRange([13, 1]) [0 seconds]
[19-05-15 10:40:31:983 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:983 MDT] Sheet.getRange([14, 1]) [0 seconds]
[19-05-15 10:40:31:983 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:984 MDT] Sheet.getRange([15, 1]) [0 seconds]
[19-05-15 10:40:31:984 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:984 MDT] Sheet.getRange([16, 1]) [0 seconds]
[19-05-15 10:40:31:985 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:985 MDT] Sheet.getRange([17, 1]) [0 seconds]
[19-05-15 10:40:31:985 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:986 MDT] Sheet.getRange([18, 1]) [0 seconds]
[19-05-15 10:40:31:986 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:987 MDT] Sheet.getRange([19, 1]) [0 seconds]
[19-05-15 10:40:31:987 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:988 MDT] Sheet.getRange([20, 1]) [0 seconds]
[19-05-15 10:40:31:988 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:988 MDT] Sheet.getRange([21, 1]) [0 seconds]
[19-05-15 10:40:31:988 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:989 MDT] Sheet.getRange([22, 1]) [0 seconds]
[19-05-15 10:40:31:989 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:990 MDT] Sheet.getRange([23, 1]) [0 seconds]
[19-05-15 10:40:31:990 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:991 MDT] Sheet.getRange([24, 1]) [0 seconds]
[19-05-15 10:40:31:991 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:992 MDT] Sheet.getRange([25, 1]) [0 seconds]
[19-05-15 10:40:31:992 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:993 MDT] Sheet.getRange([26, 1]) [0 seconds]
[19-05-15 10:40:31:993 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:993 MDT] Sheet.getRange([27, 1]) [0 seconds]
[19-05-15 10:40:31:994 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:994 MDT] Sheet.getRange([28, 1]) [0 seconds]
[19-05-15 10:40:31:994 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:995 MDT] Sheet.getRange([29, 1]) [0 seconds]
[19-05-15 10:40:31:995 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:995 MDT] Sheet.getRange([30, 1]) [0 seconds]
[19-05-15 10:40:31:995 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:996 MDT] Sheet.getRange([31, 1]) [0 seconds]
[19-05-15 10:40:31:996 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:996 MDT] Sheet.getRange([32, 1]) [0 seconds]
[19-05-15 10:40:31:997 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:997 MDT] Sheet.getRange([33, 1]) [0 seconds]
[19-05-15 10:40:31:997 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:998 MDT] Sheet.getRange([34, 1]) [0 seconds]
[19-05-15 10:40:31:998 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:999 MDT] Sheet.getRange([35, 1]) [0 seconds]
[19-05-15 10:40:31:999 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:31:999 MDT] Sheet.getRange([36, 1]) [0 seconds]
[19-05-15 10:40:32:000 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:000 MDT] Sheet.getRange([37, 1]) [0 seconds]
[19-05-15 10:40:32:000 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:001 MDT] Sheet.getRange([38, 1]) [0 seconds]
[19-05-15 10:40:32:001 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:002 MDT] Sheet.getRange([39, 1]) [0 seconds]
[19-05-15 10:40:32:002 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:002 MDT] Sheet.getRange([40, 1]) [0 seconds]
[19-05-15 10:40:32:002 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:003 MDT] Sheet.getRange([41, 1]) [0 seconds]
[19-05-15 10:40:32:003 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:004 MDT] Sheet.getRange([42, 1]) [0 seconds]
[19-05-15 10:40:32:004 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:005 MDT] Sheet.getRange([43, 1]) [0 seconds]
[19-05-15 10:40:32:005 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:006 MDT] Sheet.getRange([44, 1]) [0 seconds]
[19-05-15 10:40:32:006 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:006 MDT] Sheet.getRange([45, 1]) [0 seconds]
[19-05-15 10:40:32:007 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:007 MDT] Sheet.getRange([46, 1]) [0 seconds]
[19-05-15 10:40:32:007 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:008 MDT] Sheet.getRange([47, 1]) [0 seconds]
[19-05-15 10:40:32:008 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:009 MDT] Sheet.getRange([48, 1]) [0 seconds]
[19-05-15 10:40:32:009 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:009 MDT] Sheet.getRange([49, 1]) [0 seconds]
[19-05-15 10:40:32:009 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:010 MDT] Sheet.getRange([50, 1]) [0 seconds]
[19-05-15 10:40:32:010 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:011 MDT] Sheet.getRange([51, 1]) [0 seconds]
[19-05-15 10:40:32:011 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:012 MDT] Sheet.getRange([52, 1]) [0 seconds]
[19-05-15 10:40:32:012 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:012 MDT] Sheet.getRange([53, 1]) [0 seconds]
[19-05-15 10:40:32:012 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:013 MDT] Sheet.getRange([54, 1]) [0 seconds]
[19-05-15 10:40:32:013 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:014 MDT] Sheet.getRange([55, 1]) [0 seconds]
[19-05-15 10:40:32:014 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:015 MDT] Sheet.getRange([56, 1]) [0 seconds]
[19-05-15 10:40:32:015 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:015 MDT] Sheet.getRange([57, 1]) [0 seconds]
[19-05-15 10:40:32:015 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:016 MDT] Sheet.getRange([58, 1]) [0 seconds]
[19-05-15 10:40:32:016 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:017 MDT] Sheet.getRange([59, 1]) [0 seconds]
[19-05-15 10:40:32:017 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:017 MDT] Sheet.getRange([60, 1]) [0 seconds]
[19-05-15 10:40:32:018 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:018 MDT] Sheet.getRange([60, 1]) [0 seconds]
[19-05-15 10:40:32:018 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:019 MDT] Sheet.getRange([60, 2]) [0 seconds]
[19-05-15 10:40:32:019 MDT] Range.getValue() [0 seconds]
[19-05-15 10:40:32:319 MDT] DriveApp.getFileById([15fjNaZy7sl-42D0ZXcct_fHvy8JKnBPkQiiaGeF6wgw]) [0.299 seconds]
[19-05-15 10:40:32:322 MDT] File.getBlob() [0.001 seconds]
[19-05-15 10:40:34:891 MDT] File.getBlob() [0.002 seconds]
[19-05-15 10:40:34:893 MDT] File.getBlob() [0.001 seconds]
[19-05-15 10:40:37:033 MDT] Blob.getName() [0 seconds]
[19-05-15 10:40:37:044 MDT] Blob.getBytes() [0.01 seconds]
[19-05-15 10:40:37:052 MDT] File.getBlob() [0.002 seconds]
[19-05-15 10:40:39:683 MDT] Blob.getName() [0 seconds]
[19-05-15 10:40:39:684 MDT] Blob.getContentType() [0 seconds]
[19-05-15 10:40:39:988 MDT] MailApp.sendEmail([email@email.com, Show Me You Know Me, Read about the owners arriving tomorrow!, {attachments=[EOE604]}]) [7.667 seconds]
[19-05-15 10:40:39:991 MDT] Execution succeeded [8.575 seconds total runtime]
答案 0 :(得分:0)
我想从循环中删除所有getValue()
调用:
do {
rowstart++; }
while (unitnum != ids.getRange(rowstart, 1).getValue());
if (unitnum == ids.getRange(rowstart, 1).getValue()) {
list.push(DriveApp.getFileById(ids.getRange(rowstart,2).getValue()));
}
我猜您也可以用indexOf()代替do / while。
答案 1 :(得分:0)
您使用哪种类型的触发器?
有一些简单的触发器限制,了解更多有关哪种类型的触发器将有助于发现问题:
https://developers.google.com/apps-script/guides/triggers/
您可能想要添加日志记录,以查看是否确实有任何触发,以及在触发失败之前可以走多远。然后,检查所有日志。
console.log ("Test Text");
可安装的触发器可以发送电子邮件: https://developers.google.com/apps-script/guides/triggers/installable