google-apps-script - 数据未写入 Google 电子表格
问题描述
我的脚本将来自标记文件夹的邮件中的“未读”传入消息复制到谷歌表,然后将其标记为已读。但有时会出现故障:脚本被执行,将字母标记为“已读”,但不写入表。也就是说,事实上,在记录器中有一条记录了这些字母的所有内容,但它们并没有写入表中。理论上,您需要检查数据是否已写入表中,然后才将字母标记为“已读”。也许代码中有一个错误会定期让自己感觉到?各位大侠帮帮我,我只是在学习。
function GmailmarkReadEnd() {
//this is just the stuff that recognizes what spreadsheet you're in
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ss.getSheets();
var sheet = ss.getSheetByName('Лист2'); //gets the right sheet
/* searches your GMail for emails matching things "label:unread" + " label:support"
*/
var query = "label:unread" + " label:support";
var threads = GmailApp.search(query);
var supportStats = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var from = messages[m].getFrom(); //from field
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(); //body field
var mId = messages[m].getId(); //id field to create the link later
if (query === "label:unread" + " label:support") {
supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId]);
Logger.log(supportStats) // The log about which he spoke.
}
}
}
if(!threads.length) return; // if there are no unread ones, do nothing.
sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,supportStats.
length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
GmailApp.markThreadsRead(threads); // marks as read
// ***Sorting Recorded Data by Date***
sheet.getRange('D:D').activate();
sheet.sort(4, false);
}
解决方案
function GmailmarkReadEnd() {
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Лист2'); //gets the right sheet
const query = "label:unread" + " label:support";
var threads = GmailApp.search(query);
var supportStats = [];
for (var i = 0; i < threads.length; i++) {
var messages = threads[i].getMessages();
for (var m = 0; m < messages.length; m++) {
var from = messages[m].getFrom(); //from field
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(); //body field
var mId = messages[m].getId(); //id field to create the link later
supportStats.push([from,to,time,subject,body,'https://mail.google.com/mail/u/0/#inbox/'+mId]);
Logger.log(supportStats) // The log about which he spoke.
}
}
if(!threads.length) return; // if there are no unread ones, do nothing.
sheet.getRange(SpreadsheetApp.getActiveSheet().getLastRow()+1,2,supportStats.length,supportStats[0].length).setValues(supportStats); //writes to the spreadsheet
GmailApp.markThreadsRead(threads); // marks as read
sheet.sort(4,false);
}
推荐阅读
- node.js - NPM node-sass 安装失败
- java - 是否可以确定发出 api 请求的机器的本地时间?
- c# - ServiceStack - SessionAs 导致 PreAuthenticate 被调用
- python - 如何按索引在多列中绘制带有散点图的数据框?
- javascript - 如何对纯二进制数据执行纯按位运算
- windows - Windows 10 中的 Robocopy 命令与德语字母(ü、ä、ö)作斗争,chcp 更改代码页显然没有效果
- codenameone - 代号一中的用户位置
- javascript - 删除输入类型数字的微调器后,为什么 mozila 允许用户输入字母值?
- intellij-idea - cucumber - 在 IntelliJ 中运行 Runner 时出现“测试被忽略”错误
- google-cloud-platform - 如何通过 REST API 自动创建 Dialogflow 代理