我正在尝试在 Google 表格中显示电子邮件的最新电子邮件正文。 该表供个人使用,是在我自己的谷歌帐户中开发的,访问我自己的 Gmail 帐户。 代码在此功能上失败:
function getMostRecentEmailInfo(emailAddress) {
var threads = GmailApp.search('from:' + emailAddress, 0, 1); // Search for emails from the specific email address
if (threads.length > 0) {
var messages = threads[0].getMessages();
var mostRecentMessage = messages[messages.length - 1]; // Get the most recent email
var emailText = mostRecentMessage.getPlainBody(); // Extract the text of the email
var emailDate = mostRecentMessage.getDate(); // Extract the date of the email
return "On: " + emailDate + ". Wrote: " + emailText
} else {
return 'No email found from ' + emailAddress;
}
}
出现此执行错误:
我认为我已获得相关授权。 appsscript.json 是:
{
"timeZone": "Europe/London",
"dependencies": {
"enabledAdvancedServices": [
{
"userSymbol": "Gmail",
"version": "v1",
"serviceId": "gmail"
}
]
},
"exceptionLogging": "STACKDRIVER",
"runtimeVersion": "V8",
"webapp": {
"executeAs": "USER_DEPLOYING",
"access": "MYSELF"
},
"oauthScopes": [
"https://www.googleapis.com/auth/gmail.readonly"
]
}
任何人都可以建议我为什么会收到此错误以及如何修复它吗?
作为参考,这里是完整的代码:
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
// Check if the edited cell is the one with the dropdown box
if (range.getA1Notation() == "B1" && sheet.getName() == "Workbench") {
var selectedValue = e.value; // Get the value that was selected in the dropdown box
if (selectedValue === "Pending") {
selectedValue = ""
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var outputSheet = ss.getSheetByName("Workbench");
if(outputSheet.getLastRow() >= 4) {//clear down the previous rows
outputSheet.getRange(4, 1, outputSheet.getLastRow() - 3, outputSheet.getLastColumn()).clearContent();
}
var outputData = [];
//Website form response data
var sheetToCopy = ss.getSheetByName("Form responses 1");
var data = sheetToCopy.getDataRange().getValues();
//Name, Email, Date, Response, Additional,Source
data.forEach(function(row) {
//status, date, email, name, question, interested in
if (row[0] === selectedValue) { // Check if the 1st column value is "Active"
var outputRow = []
outputRow[0] = row[3]
outputRow[1] = createGmailHyperlink(row[2])
outputRow[2] = row[1]
outputRow[3] = row[4]
outputRow[4] = row[5]
outputRow[5] = "Website Form"
outputRow[6] = getMostRecentEmailInfo(row[2])
outputData.push(outputRow);
}
});
//BSAC Find It tool responses
sheetToCopy = ss.getSheetByName("BSAC Responses");
data = sheetToCopy.getDataRange().getValues();
//Name, Email, Date, Response, Additional,Source
data.forEach(function(row) {
//status, date, name, email, telephone, interested in, additional details
if (row[0] === selectedValue) { // Check if the 1st column value is "Active"
var outputRow = []
outputRow[0] = row[2]
outputRow[1] = createGmailHyperlink(row[3])
outputRow[2] = row[1]
outputRow[3] = row[6]
outputRow[4] = row[5]
outputRow[5] = "BSAC Find It"
//outputRow[6] = getMostRecentEmailInfo(row[3])
outputData.push(outputRow);
}
});
//Direct
sheetToCopy = ss.getSheetByName("Direct");
data = sheetToCopy.getDataRange().getValues();
//Name, Email, Date, Response, Additional,Source
data.forEach(function(row) {
//status, date, name, email, telephone, interested in
if (row[0] === selectedValue) { // Check if the 1st column value is "Active"
var outputRow = []
outputRow[0] = row[2]
outputRow[1] = createGmailHyperlink(row[3])
outputRow[2] = row[1]
outputRow[3] = row[5]
outputRow[4] = ""
outputRow[5] = "Direct"
//outputRow[6] = getMostRecentEmailInfo(row[3])
outputData.push(outputRow);
}
});
if (outputData.length > 0) {
outputSheet.getRange(outputSheet.getLastRow() + 1, 1, outputData.length, outputData[0].length).setValues(outputData);
}
}
}
function createGmailHyperlink(emailAddress) {
var gmailLink = 'https://mail.google.com/mail/u/0/#search/to%3A' + encodeURIComponent(emailAddress) + '+OR+from%3A' + encodeURIComponent(emailAddress);
var hyperlinkFormula = '=HYPERLINK("' + gmailLink + '", "' + emailAddress + '")';
return hyperlinkFormula;
}
function getMostRecentEmailInfo(emailAddress) {
var threads = GmailApp.search('from:' + emailAddress, 0, 1); // Search for emails from the specific email address
if (threads.length > 0) {
var messages = threads[0].getMessages();
var mostRecentMessage = messages[messages.length - 1]; // Get the most recent email
var emailText = mostRecentMessage.getPlainBody(); // Extract the text of the email
var emailDate = mostRecentMessage.getDate(); // Extract the date of the email
return "On: " + emailDate + ". Wrote: " + emailText
} else {
return 'No email found from ' + emailAddress;
}
}
我发现这非常有帮助。 我没有将触发器定义为可安装触发器,因此,尽管它在我想要的时候触发(在编辑电子表格单元格时),但它没有所需的授权。 一旦我将 onEdit(e) 链接到可安装触发器(时钟图标),该工作表就需要授权,并且现在可以按预期工作。