Google Apps脚本Gmail CSV导入到表错误
我已经从各种Google搜索中拼凑了此代码,如果电子邮件具有特定的标签,这些代码将摘取电子邮件的CSV附件。
function importCSVFromGmail() {
//gets first(latest) message with set label
var threads = GmailApp.getUserLabelByName('Dashboard Updates').getThreads(0,1);
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
if (attachment.getContentType() === "text/csv") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Monthly_Detail_Instantis");
//parses content of csv to array
var dataString = attachment.getDataAsString();
var escapedString = dataString.replace(/(?=["'])(?:"[^"\\]*(?:\\[\s\S][^"\\]*)*"|'[^'\\]\r\n(?:\\[\s\S][^'\\]\r\n)*')/g, '\\r\\n');
var csvData = Utilities.parseCsv(escapedString);
// Remember to clear the content of the sheet before importing new data
sh.clearContents().clearFormats();
//pastes array to sheet
sh.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
//marks the Gmail message as read and unstars it (Filter sets a star)
message.markRead();
message.unstar();
}
该脚本运行良好,但是我正在遇到具有带有逗号或引号的值的单元格的问题。例如,如果单元格具有以下内容:
1,000,000
或者
Google "Apps" Script
将分别返回以下内容。
\r\n
\r\n\r\n\r\n
我敢肯定这与使用的正则义务有关,但是我不确定如何调整上述条件。对此的任何帮助将不胜感激。
I've pieced together this code from various google searches that will pull an e-mail's CSV attachment if the e-mail has a specific label.
function importCSVFromGmail() {
//gets first(latest) message with set label
var threads = GmailApp.getUserLabelByName('Dashboard Updates').getThreads(0,1);
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
if (attachment.getContentType() === "text/csv") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Monthly_Detail_Instantis");
//parses content of csv to array
var dataString = attachment.getDataAsString();
var escapedString = dataString.replace(/(?=["'])(?:"[^"\\]*(?:\\[\s\S][^"\\]*)*"|'[^'\\]\r\n(?:\\[\s\S][^'\\]\r\n)*')/g, '\\r\\n');
var csvData = Utilities.parseCsv(escapedString);
// Remember to clear the content of the sheet before importing new data
sh.clearContents().clearFormats();
//pastes array to sheet
sh.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
}
//marks the Gmail message as read and unstars it (Filter sets a star)
message.markRead();
message.unstar();
}
The script runs fine, however I am running into issues with cells that had values with commas or quotes. For example, if a cell has the following:
1,000,000
or
Google "Apps" Script
It will return to following, respectively.
\r\n
\r\n\r\n\r\n
I'm certain it has to do with the Regex used, however I am not certain how to adjust for the above. Any help with this would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我能够使用原始代码(使用了有问题的代码),并用此链接中的代码替换了Escaping and Utilities.parsecsv。即使细胞包括引号和逗号,这也将适当地导入CSV:
https://productforums.google.com/forum/#!topic/docs/ nhxjrl8jiek
这是我的最终代码:
I was able to use the original code (used in question) and replace the escaping and Utilities.parseCsv with code from this link. This will properly import CSVs even if cells include quotes and commas:
https://productforums.google.com/forum/#!topic/docs/nhXjrl8JIek
Here is my final code: