GmailApp 错误:在某个上下文中调用过多次,但在另一个上下文中则没有调用
当我尝试简单明了时,它会通过,证明我没有超出每日配额:
function email() {
var now = new Date();
GmailApp.sendEmail("[email protected]", "current time", "The time is: " + now.toString());
}
但是,当用户连续勾选一个复选框时,脚本会获取该行的一些数据来撰写电子邮件发送。这在编辑基础上运行(可安装触发器),并抛出 am 错误:
function sendEmail() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
//Escapes the script if not in the right tab
if (sheet.getName() != 'Todays Tests V2') {
return;
}
//Defines ranges/criteria
var testSheet = ss.getSheetByName("Todays Tests V2");
var row = sheet.getActiveCell().getRow();
var col = sheet.getActiveCell().getColumn();
var sendResults = testSheet.getRange(row, 15, 1, 1).getValue();
//Gets data to compare against row getting a checkbox ticked/script fired
var dbSheet = ss.getSheetByName('Database');
var dbDataRng = dbSheet.getRange(2, 13, dbSheet.getLastRow() - 1, 2);
var dbData = dbDataRng.getValues();
if (ss.getActiveSheet().getSheetName() == testSheet.getSheetName() &&
row > 5 &&
col == 15 &&
sendResults == true) {
var lock = LockService.getScriptLock();
try {
lock.tryLock(4000); // wait 04 seconds for others' use of the code section and lock to stop and then proceed
} catch (e) {
Logger.log('Could not obtain lock after 04 seconds.');
return HtmlService.createHtmlOutput("<b> Server Busy. Please try after some time <p>");
}
//Defines variables from the row being edited
var email = testSheet.getRange(row, 5).getValue();
var fullName = testSheet.getRange(row, 3).getValue() + ' ' + testSheet.getRange(row, 4).getValue();
var testNo = testSheet.getRange(row, 2).getValue();
//Sends the email
GmailApp.sendEmail(email, "SUBJECT", fullName + " blah blah blah.", { name: 'YOUR SERVICE PROVIDER' });
for (var n = 0; n < dbData.length; n++) {
if (dbData[n][0] == testNo) {
dbSheet.getRange('N' + (2 + n)).setValue('Yes');
}
}
lock.releaseLock();
}
} catch (err) {
Logger.log('Erro: ' + err)
testSheet.getRange(row, 15).setValue('FALSE');
ss.toast('Error: ' + err);
}
}
日志正确显示 email
和 fullName
。 感谢任何对造成这种情况的可能性的了解。
When I try it plain and simple, it goes through, proving my I haven't exceeded my daily quota:
function email() {
var now = new Date();
GmailApp.sendEmail("[email protected]", "current time", "The time is: " + now.toString());
}
However, when the user is ticks a checkbox in a row, the script gets some of that row's data to compose an email to be sent. This runs on edit basis (Installable Trigger) and it throws the am error:
function sendEmail() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
const sheet = ss.getActiveSheet();
//Escapes the script if not in the right tab
if (sheet.getName() != 'Todays Tests V2') {
return;
}
//Defines ranges/criteria
var testSheet = ss.getSheetByName("Todays Tests V2");
var row = sheet.getActiveCell().getRow();
var col = sheet.getActiveCell().getColumn();
var sendResults = testSheet.getRange(row, 15, 1, 1).getValue();
//Gets data to compare against row getting a checkbox ticked/script fired
var dbSheet = ss.getSheetByName('Database');
var dbDataRng = dbSheet.getRange(2, 13, dbSheet.getLastRow() - 1, 2);
var dbData = dbDataRng.getValues();
if (ss.getActiveSheet().getSheetName() == testSheet.getSheetName() &&
row > 5 &&
col == 15 &&
sendResults == true) {
var lock = LockService.getScriptLock();
try {
lock.tryLock(4000); // wait 04 seconds for others' use of the code section and lock to stop and then proceed
} catch (e) {
Logger.log('Could not obtain lock after 04 seconds.');
return HtmlService.createHtmlOutput("<b> Server Busy. Please try after some time <p>");
}
//Defines variables from the row being edited
var email = testSheet.getRange(row, 5).getValue();
var fullName = testSheet.getRange(row, 3).getValue() + ' ' + testSheet.getRange(row, 4).getValue();
var testNo = testSheet.getRange(row, 2).getValue();
//Sends the email
GmailApp.sendEmail(email, "SUBJECT", fullName + " blah blah blah.", { name: 'YOUR SERVICE PROVIDER' });
for (var n = 0; n < dbData.length; n++) {
if (dbData[n][0] == testNo) {
dbSheet.getRange('N' + (2 + n)).setValue('Yes');
}
}
lock.releaseLock();
}
} catch (err) {
Logger.log('Erro: ' + err)
testSheet.getRange(row, 15).setValue('FALSE');
ss.toast('Error: ' + err);
}
}
The log shows email
and fullName
correctly.
Appreciate any light shed to the possibility causing this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 getRemainingDailyQuota()查看有效用户每日剩余邮件配额。
您也可以尝试类似的方法来跟踪可安装触发器的总运行时间。
来自 https://developers.google.com/apps-script/guides/services /quotas
和 G Suite 免费版(旧版)
帐户
需要明确的是,脚本配额取决于有效用户,而不是脚本所有者。
如果脚本是从自定义菜单或简单触发器运行的,则活动用户和有效用户是相同的。
如果脚本由可安装的触发器触发,则有效用户是安装触发器的用户,而不是脚本所有者或正在编辑/更改电子表格(即通过单击复选框)的用户。
从编辑器、自定义菜单、按钮(分配了脚本的图像)或对话框/侧边栏运行的脚本没有总运行时配额。
另一方面,当使用复选框或单元格编辑时,避免非常快速地进行更改,即避免立即依次单击复选框,单击一个复选框后等待几秒钟,然后再单击下一个。
相关
Use getRemainingDailyQuota() to check the effective user remaining daily email quota.
Also you might try to something similar to track the total runtime of your installable triggers.
From https://developers.google.com/apps-script/guides/services/quotas
and G Suite free edition (legacy)
accounts
To be clear, script quotas depend on the effective user, not on the script owner.
If the script is ran from a custom menu or simple trigger the active user and the effective user are the same.
If the script is triggered by an installable trigger, the effective user is the user who installed the trigger, not the script owner either the user who is editing / making changes to the spreadsheet i.e. by clicking a checkbox.
Scripts ran from the editor, from a custom menu, a button (image with a script assigned) or a dialog / sidebar haven't a total runtime quota.
By the other hand when using checkboxes or cell edits avoid doing changes very fast, i.e. avoid clicking checkboxes one after the other immediately, after clicking on a checkbox wait few seconds before clicking the next one.
Related