英文:
Spreadsheet Updates Automatically When Email Is Received, (pub/sub) But with Wrong information
问题
我在Google Cloud Platform上设置了我的第一个Pub/Sub,它使用Gmail API来在每次收到电子邮件时更新我的电子表格。
然而,下面的doPost函数在我的电子表格上返回类似于以下内容:
"8/4/2023 10:01:59"、"8011555761044464"、"{"emailAddress":"myemail@gmail.com","historyId":37309620}"
我仍然是新手,但我希望电子表格中有发件人的信息和主题行。我该如何改进我的脚本?
// ************************* POST *************************
function doPost (e) {
const message = JSON.parse(e.postData.getDataAsString()).message;
const data = Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString();
const ss = SpreadsheetApp.openById("MYSHEET").getSheetByName("Borghers Emails");
ss.appendRow([new Date(), message.message_id, data]);
return 200;
};
// ************************* ENROLL EMAIL *************************
function enrollEmail() {
try {
const watchRes = {
labelIds: ["INBOX"],
labelFilterAction: "include",
topicName: "projects/myproject/topics/gmailTrigger"
};
const response = Gmail.Users.watch(watchRes, "me");
console.log(response);
} catch (error) {
console.log(error);
}
};
编辑:这是更新后的代码 这实际上有效,谢谢Tanaike!然而,我认为Pub/Sub和我的脚本之间存在误解,因为当我发送一封测试电子邮件时,电子表格将进入无限循环,不会停止。我不确定如何与Pub/Sub通信以确保每次只发生一条消息。请参见下面的我的脚本和我的电子表格结果:
function doPost(e) {
const message = JSON.parse(e.postData.getDataAsString()).message;
const data = JSON.parse(Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString());
const { historyId } = data;
const { history } = Gmail.Users.History.list("me", { startHistoryId: historyId });
if (history.length > 0) {
const messageId = history[0].messages[0].id;
const msg = GmailApp.getMessageById(messageId);
const ss = SpreadsheetApp.openById("MYSHEET").getSheetByName("Email Log");
ss.appendRow([new Date(), messageId, msg.getFrom(), msg.getSubject()]);
}
return ContentService.createTextOutput("ok");
}
英文:
I set up my first Pub/Sub on Google Cloud Platform that uses Gmail API to update my spreadsheet each time I receive an email
(More information on how to do this in my previous question)
However, the below doPost function returns something like this on my spreadsheet:
"8/4/2023 10:01:59", "8011555761044464", "{"emailAddress":"myemail@gmail.com","historyId":37309620}"
I am still quite new, but I'd like the spreadsheet to have the sender's information and the subject line. How can I improve my script?
// ************************* POST *************************
function doPost (e) {
const message = JSON.parse(e.postData.getDataAsString()).message;
const data = Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString();
const ss = SpreadsheetApp.openById("MYSHEET").getSheetByName("Borghers Emails");
ss.appendRow([new Date(), message.message_id, data]);
return 200;
};
// ************************* ENROLL EMAIL *************************
function enrollEmail() {
try {
const watchRes = {
labelIds: ["INBOX"],
labelFilterAction: "include",
topicName: "projects/myproject/topics/gmailTrigger"
};
const response = Gmail.Users.watch(watchRes, "me");
console.log(response);
} catch (error) {
console.log(error);
}
};
EDIT: HERE IS THE UPDATED CODE This actually works, thank you Tanaike! However, I think there is a miscommunication between the Pub/Sub and my script because when I send one test email, the spreadsheet will go into an infinite loop where it is updating the sheet and will not stop. I am uncertain how to communicate with the Pub/Sub to ensure only ONE message occurs at a time. See below for my script and the result on my sheet:
function doPost(e) {
const message = JSON.parse(e.postData.getDataAsString()).message;
const data = JSON.parse(Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString());
const { historyId } = data;
const { history } = Gmail.Users.History.list("me", { startHistoryId: historyId });
if (history.length > 0) {
const messageId = history[0].messages[0].id;
const msg = GmailApp.getMessageById(messageId);
const ss = SpreadsheetApp.openById("MYSHEET").getSheetByName("Email Log");
ss.appendRow([new Date(), messageId, msg.getFrom(), msg.getSubject()]);
}
return ContentService.createTextOutput("ok");
}
答案1
得分: 2
I believe your goal is as follows.
- 您已经完成了将电子邮件推送到 Web 应用程序并使用发布/订阅机制的设置。
- 您希望通过修改脚本来检索电子邮件的电子邮件地址和电子邮件标题。
In this case, how about the following modification?
Modified script:
请在高级 Google 服务中启用 Gmail API。
function doPost(e) {
const message = JSON.parse(e.postData.contents).message;
const data = JSON.parse(Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString());
const { historyId } = data;
const { history } = Gmail.Users.History.list("me", { startHistoryId: historyId });
if (history.length > 0) {
const messageId = history[0].messages[0].id;
const msg = GmailApp.getMessageById(messageId);
const ss = SpreadsheetApp.openById("MYSHEET").getSheetByName("Borghers Emails");
ss.appendRow([new Date(), messageId, msg.getFrom(), msg.getSubject()]);
}
return ContentService.createTextOutput("ok");
}
-
在此修改后的脚本中,当接收到电子邮件时,将使用 pub/sub 的事件对象运行
doPost
。然后,从事件对象中检索历史记录 ID。接下来,从最新的历史记录 ID 中检索消息 ID。然后检索消息 ID、发件人和消息标题,并将这些值附加到名为 "Borghers Emails" 的工作表中。 -
在这种情况下,您脚本中的
message.message_id
不同于电子邮件的消息 ID。那是 pub/sub 消息 ID。请注意这一点。
注意:
-
当您修改 Web Apps 的 Google Apps 脚本时,请将部署更改为新版本。通过这种方式,修改后的脚本将反映在 Web Apps 中。请注意这一点。
-
您可以在我的报告 "在不更改 Web Apps 的 URL 的情况下重新部署 Web Apps 的新 IDE(作者:我)" 中详细了解这一点。
参考:
英文:
I believe your goal is as follows.
- You have already finished setting for pushing to your Web Apps with pub/sub when an email is received.
- You want to retrieve the email address of the email and the email title by modifying your script.
In this case, how about the following modification?
Modified script:
Please enable Gmail API at Advanced Google services.
function doPost(e) {
const message = JSON.parse(e.postData.contents).message;
const data = JSON.parse(Utilities.newBlob(Utilities.base64Decode(message.data)).getDataAsString());
const { historyId } = data;
const { history } = Gmail.Users.History.list("me", { startHistoryId: historyId });
if (history.length > 0) {
const messageId = history[0].messages[0].id;
const msg = GmailApp.getMessageById(messageId);
const ss = SpreadsheetApp.openById("MYSHEET").getSheetByName("Borghers Emails");
ss.appendRow([new Date(), messageId, msg.getFrom(), msg.getSubject()]);
}
return ContentService.createTextOutput("ok");
}
-
In this modified script, when an email is received,
doPost
is run with the event object from pub/sub. And, the history ID is retrieved from the event object. And then, the message ID is retrieved from the latest history ID. And the message ID, from, and the message title are retrieved, and those values are appended to the sheet "Borghers Emails". -
In this case,
message.message_id
in your script is not the same as the message ID of the email. That is pub/sub message ID. Please be careful about this.
Note:
-
When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
-
You can see the detail of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".
Reference:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论