我使用了https://webapps.stackexchange.com/questions/160232/gmail-bulk-emails-manipulation并稍作修改以將大約 90,000 封電子郵件(來自免費的 gmail 帳戶)復制到一張紙上。
該腳本只作業一次。一個周期后,觸發器變為“禁用”并顯示“未知原因”。
我減少了批量大小,增加了時間間隔,但仍然出現相同的錯誤。
我哪里錯了?
/**
* Creates the first trigger to call batchArchiveEmail.
*/
function init(){
var Triggers = ScriptApp.getProjectTriggers();
for (var p = 0; p < Triggers.length; p ) {
ScriptApp.deleteTrigger(Triggers[p])
}
ScriptApp
.newTrigger('batchArchiveEmail')
.timeBased()
.after(60 * 1000)
.create();
console.log(`trigger created`)
}
/**
* Archive emails by batches preventing controlling limiting the execution time and
* creating a trigger if there are still threads pending to be archived.
*/
function batchArchiveEmail(){
const start = Date.now();
/**
* Own execution time limit for the search and archiving operations to prevent an
* uncatchable error. As the execution time check is done in do..while condition there
* should be enough time to one search and archive operation and to create a trigger
* to start a new execution.
*/
const maxTime = 3 * 60 * 1000; // Instead of 25 use 3 for Google free accounts
const batchSize = 50;
let threads, elapsedTime;
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName("Sheet1");
/** Search and archive threads, then repeat until the search returns 0 threads or the
* maxTime is reached
*/
var ms=[];
do {
threads = GmailApp.search('label:inbox before:2022/5/1');
for (let j = 0; j < threads.length; j = batchSize) {
//below code added by me
ms=[];
var messages = threads[j].getMessages();
for (var m = 0; m < messages.length; m ) {
var from = messages[m].getFrom(); //from field
var mId = messages[m].getId();//id field to create the link later
var supportStats = [];
var to = messages[m].getTo();//to field
var time = messages[m].getDate();//date field
var subject = messages[m].getSubject();//subject field
var body=messages[m].getPlainBody();
var tel=[];
tel = body.match(/[\ ]?\d{10}|\(\d{3}\)\s?-\d{6}|\d{3}\s-\d{3}\s\d{4}/);
supportStats.push(from);
supportStats.push(to);
supportStats.push(time);
supportStats.push(subject);
supportStats.push('https://mail.google.com/mail/u/0/#inbox/' mId); //build the URL to the email
supportStats.push(body);
if (tel){supportStats.push(tel[0])} else {supportStats.push("")};
ms.push(supportStats);
}
var lr=sheet.getLastRow();
sheet.getRange(lr 1,1,ms.length,7).setValues(ms);
//above code added by me
GmailApp.moveThreadsToArchive(threads.slice(j, j batchSize));
};
/**
* Used to prevent to have too many calls in a short time, might not be
* necessary with a large enough batchSize
*/
Utilities.sleep(`2000`);
elapsedTime = Date.now() - start;
} while (threads.length > 0 && elapsedTime < maxTime);
if(threads.length > 0){
/** Delete the last trigger */
var Triggers = ScriptApp.getProjectTriggers();
for (var p = 0; p < Triggers.length; p ) {
ScriptApp.deleteTrigger(Triggers[p])
}
//deleteTriggers();
/** Create a one-time new trigger */
ScriptApp
.newTrigger('batchArchiveEmail')
.timeBased()
.after(300 * 1000)
.create();
console.log(`next trigger created`)
} else {
/** Delete the last trigger */
var Triggers = ScriptApp.getProjectTriggers();
for (var p = 0; p < Triggers.length; p ) {
ScriptApp.deleteTrigger(Triggers[p])
}
console.log(`No more threads to process`);
}
}
uj5u.com熱心網友回復:
問題和解決方法:
當我測驗你的腳本時,我和你確認了同樣的情況。在這種情況下,不幸的是,即使增加下一次觸發時間,也無法避免該問題。
在這個答案中,我想介紹一個我已經在這個執行緒上發布的解決方法。不幸的是,我的回答對該執行緒沒有用。但是,幸運的是,當我測驗您的腳本時,我確認可以使用此解決方法。
關于The newTrigger is never running.,如果您在腳本編輯器中使用 V8 運行時并且安裝的觸發器不起作用,我擔心這可能是錯誤。參考 1 參考 2
但是,幸運的是,在當前階段,有一種解決方法可以消除此錯誤。它是使用網??絡應用程式。參考我以前也遇到過同樣的情況。此解決方法的流程如下。
- 運行要運行的腳本。
- 安裝時間驅動觸發器時,即使用 Web 應用程式安裝。
- 這是重要的一點。
在這種情況下,將運行腳本安裝的時間驅動觸發器。通過這種解決方法,您的腳本可以啟用 V8 運行時。當你的腳本被修改后,它變成如下。
用法:
在此解決方法中,使用了 Web 應用程式。所以,請執行以下流程。
1. 部署 Web 應用程式。
- 在腳本編輯器上,在腳本編輯器的右上角,請點擊“點擊部署”->“新建部署”。
- 請點擊“選擇型別”->“Web App”。
- 請在“部署配置”下的欄位中輸入有關 Web App 的資訊。
- 請為“執行為”選擇“我” 。
- 請為“誰有權訪問”選擇“僅限我自己” 。
- 請點擊“部署”按鈕。并且,單擊“完成”按鈕。
- 在腳本編輯器上,在腳本編輯器的右上角,請單擊“單擊部署”->“測驗部署”。
- 請復制 Web Apps 的 URL。
- 在這種情況下,URL 就像
https://script.google.com/macros/s/###/dev.
- 在這種情況下,URL 就像
詳細資訊可以看官方檔案和我的報告。
2.修改腳本。
請按如下方式修改您的腳本。請將您的 Web Apps URL 設定為webAppsUrl。
const functionName = "batchArchiveEmail"; // This is the function name for executing.
const webAppsUrl = "https://script.google.com/macros/s/###/dev"; // Please set your Web Apps URL.
// When v8 runtime is used, when the trigger is set from the function executing by a trigger, the trigger is disabled. This is the recognized bug. But unfortunately, this has still not been resolved. (September 21, 2021)
// https://issuetracker.google.com/issues/150756612
// https://issuetracker.google.com/issues/142405165
const doGet = _ => {
deleteTriggers();
ScriptApp.newTrigger(functionName).timeBased().after(60000).create();
return ContentService.createTextOutput();
// DriveApp.getFiles(); // This is used for automatically detecting the scopes for requesting to Web Apps. Please don't remove this comment line.
};
const deleteTriggers = _ => {
ScriptApp.getProjectTriggers().forEach(e => {
if (e.getHandlerFunction() == functionName) {
ScriptApp.deleteTrigger(e);
}
});
}
// Please run this function.
function init() {
deleteTriggers();
UrlFetchApp.fetch(webAppsUrl, { headers: { authorization: "Bearer " ScriptApp.getOAuthToken() } });
console.log(`trigger created`)
}
/**
* Archive emails by batches preventing controlling limiting the execution time and
* creating a trigger if there are still threads pending to be archived.
*/
function batchArchiveEmail() {
const start = Date.now();
/**
* Own execution time limit for the search and archiving operations to prevent an
* uncatchable error. As the execution time check is done in do..while condition there
* should be enough time to one search and archive operation and to create a trigger
* to start a new execution.
*/
const maxTime = 3 * 60 * 1000; // Instead of 25 use 3 for Google free accounts
const batchSize = 50;
let threads, elapsedTime;
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName("Sheet1");
/** Search and archive threads, then repeat until the search returns 0 threads or the
* maxTime is reached
*/
var ms=[];
do {
threads = GmailApp.search('label:inbox before:2022/5/1');
for (let j = 0; j < threads.length; j = batchSize) {
//below code added by me
ms=[];
var messages = threads[j].getMessages();
for (var m = 0; m < messages.length; m ) {
var from = messages[m].getFrom(); //from field
var mId = messages[m].getId();//id field to create the link later
var supportStats = [];
var to = messages[m].getTo();//to field
var time = messages[m].getDate();//date field
var subject = messages[m].getSubject();//subject field
var body=messages[m].getPlainBody();
var tel=[];
tel = body.match(/[\ ]?\d{10}|\(\d{3}\)\s?-\d{6}|\d{3}\s-\d{3}\s\d{4}/);
supportStats.push(from);
supportStats.push(to);
supportStats.push(time);
supportStats.push(subject);
supportStats.push('https://mail.google.com/mail/u/0/#inbox/' mId); //build the URL to the email
supportStats.push(body);
if (tel){supportStats.push(tel[0])} else {supportStats.push("")};
ms.push(supportStats);
}
var lr=sheet.getLastRow();
sheet.getRange(lr 1,1,ms.length,7).setValues(ms);
//above code added by me
GmailApp.moveThreadsToArchive(threads.slice(j, j batchSize));
};
/**
* Used to prevent to have too many calls in a short time, might not be
* necessary with a large enough batchSize
*/
Utilities.sleep(2000);
elapsedTime = Date.now() - start;
} while (threads.length > 0 && elapsedTime < maxTime);
if (threads.length > 0) {
/** Delete the last trigger */
deleteTriggers();
/** Create a one-time new trigger */
UrlFetchApp.fetch(webAppsUrl, { headers: { authorization: "Bearer " ScriptApp.getOAuthToken() } });
console.log(`next trigger created`)
} else {
/** Delete the last trigger */
deleteTriggers();
console.log(`No more threads to process`);
}
}
3. 測驗。
在修改后的腳本中,作為第一次運行,請init()使用腳本編輯器運行。這樣,您的腳本就會運行并使用 Web 應用程式安裝時間驅動的觸發器。并且,已安裝的觸發器由時間驅動觸發器自動運行。
筆記:
- 在這個修改后的腳本中,它假設您的函式
batchArchiveEmail()作業正常。請注意這一點。 - 如果您禁用 V8 運行時,我認為腳本可能在沒有上述修改的情況下作業。但是那樣的話,回圈的處理成本就會變高。參考通過這個,我想介紹這個解決方法。
參考:
- 使用 Google Apps 腳本在特定時間執行帶有分鐘計時器的函式
uj5u.com熱心網友回復:
這里This trigger has been disable for an unknown reason報告了的問題,并且似乎與. 單擊讓 Google 知道您也受到該問題的影響。V8 runtime 1
停用Project Settings > General Settings > Enable Chrome V8 runtime似乎可以解決某些用戶的問題。appsscript.json與從以下位置更改密鑰相同runtimeVersion:
"runtimeVersion": "V8"
至
"runtimeVersion": "DEPRECATED_ES5"
uj5u.com熱心網友回復:
我測驗了問題中的代碼,稍作更改 b/c 我不想歸檔收件箱中的所有訊息。它作業正常。
更改只是在 上添加了更具體的搜索查詢threads = GmailApp.search('label:inbox before:2022/5/1');。更改如下所示:
threads = GmailApp.search(`label:inbox before:2022/5/1 from:"Sender Display Name"`);
"Sender Display Name"我在主要類別中獲得的時事通訊的顯示名稱在哪里。
考慮到這個問題中的其他答案(在撰寫本文時,1和2)以及此處的其他相關問題和其他地方的帖子,看起來谷歌有一些失敗,也許它影響了少數專案/用戶。來自評論 #199,日期為 2022 年 6 月 7 日 06:49AM CDT,
你好,
對于受此影響的任何人,您能否提供受影響的腳本 ID?
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/489497.html
上一篇:當源單元格為公式時,Google表格圖表軸標題不會更新
下一篇:批量翻譯Google表格中的行
