我发现此脚本运行正常,只是它不会从最近的电子邮件中提取数据,而只会从第一封电子邮件中提取数据。如果有人可以帮助我将其配置为在拉出.csv附件后删除电子邮件,那将是完美的选择。
谢谢!
function importCSVFromGmail() {
//gets first(latest) message with set label
var threads = GmailApp.getUserLabelByName('South Loop Report').getThreads(0,1);
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
attachment.setContentTypeFromExtension();
if (attachment.getContentType() === "text/csv") {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Sheet1");
//parses content of csv to array
var dataString = attachment.getDataAsString();
var csvData = CSVToArray(dataString);
// Remember to clear the content of the sheet before importing new data
sh.clearContents().clearFormats();
//pastes array to sheet
var lastRowValue = sh.getLastRow();
for (var i = 0; i < csvData.length; i++) {
sh.getRange(i+lastRowValue+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
}
}
//marks the Gmail message as read and unstars it (Filter sets a star)
message.markRead();
message.unstar();
}
//The code formats the code so it can be entered into the Google Script
function CSVToArray( strData, strDelimiter ){
// Check to see if the delimiter is defined. If not,
// then default to comma.
strDelimiter = (strDelimiter || ",");
// Create a regular expression to parse the CSV values.
var objPattern = new RegExp(
(
// Delimiters.
"(\\" + strDelimiter + "|\\r?\\n|\\r|^)" +
// Quoted fields.
"(?:\"([^\"]*(?:\"\"[^\"]*)*)\"|" +
// Standard fields.
"([^\"\\" + strDelimiter + "\\r\\n]*))"
),
"gi"
);
// Create an array to hold our data. Give the array
// a default empty first row.
var arrData = [[]];
// Create an array to hold our individual pattern
// matching groups.
var arrMatches = null;
// Keep looping over the regular expression matches
// until we can no longer find a match.
while (arrMatches = objPattern.exec( strData )){
// Get the delimiter that was found.
var strMatchedDelimiter = arrMatches[ 1 ];
// Check to see if the given delimiter has a length
// (is not the start of string) and if it matches
// field delimiter. If id does not, then we know
// that this delimiter is a row delimiter.
if (
strMatchedDelimiter.length &&
(strMatchedDelimiter != strDelimiter)
){
// Since we have reached a new row of data,
// add an empty row to our data array.
arrData.push( [] );
}
// Now that we have our delimiter out of the way,
// let's check to see which kind of value we
// captured (quoted or unquoted).
if (arrMatches[ 2 ]){
// We found a quoted value. When we capture
// this value, unescape any double quotes.
var strMatchedValue = arrMatches[ 2 ].replace(
new RegExp( "\"\"", "g" ),
"\""
);
} else {
// We found a non-quoted value.
var strMatchedValue = arrMatches[ 3 ];
}
// Now that we have our value string, let's add
// it to the data array.
arrData[ arrData.length - 1 ].push( strMatchedValue );
}
// Return the parsed data.
return( arrData );
var label = GmailApp.getUserLabelByName("South Loop Report");
label.deleteLabel();
// Move the first message in your inbox to trash
var firstThread = GmailApp.getInboxThreads(0,1)[0];
var firstMessage = firstThread.getMessages()[0];
GmailApp.moveMessageToTrash(firstMessage);
}
同样,代码正常工作,我只需要它在处理csv文件后就删除电子邮件,因此我可以为其运行自动触发器以每5分钟从新电子邮件中提取附件。
谢谢!
答案 0 :(得分:2)
importCSVFromGmail
中最后一个消息。如果我的理解是正确的,那么该修改如何?
请在函数importCSVFromGmail
中添加以下脚本,如下所示。使用此功能时,please enable Gmail API at Advanced Google services.
message.markRead();
message.unstar();
}
至:
message.markRead();
message.unstar();
Gmail.Users.Messages.remove("me", message.getId()); // Added
}
CSVToArray
的功能中,return( arrData );
不会运行return( arrData );
下面的脚本。请注意这一点。如果我误解了您的问题,而这不是您想要的结果,我深表歉意。