我创建了一个脚本,该脚本会自动响应在休息日发送给我的电子邮件。我最近决定对其进行升级,以便通过发送特殊格式的电子邮件,将数据从正文中提取出来,将这些数据存储在Google表格中,然后提取该值并将其用作我的休假日。Google Apps脚本将问题分析编号转换为变量
我几乎所有的工作方式,我需要它,但是当我从单元格中读取的值传递给变量时,它不起作用。但是,当我将它设置为代码中的静态数字时,它工作正常。我认为这是因为它被当作一个字符串来读取,所以我将它分开,并将它们专门编入数字中。但是,这也没有奏效。所以现在我被卡住了。任何帮助,将不胜感激。
这里是我的代码:
function autoResponder(){
var content;
var myemail = Session.getActiveUser().getEmail();
//Searches your Google Drive for the spreasdsheet that will hold your day off values.
var searchDrive = DriveApp.searchFiles('title contains "AutoResponder Data"')
if (searchDrive.hasNext()){
var spreadsheet = SpreadsheetApp.open(searchDrive.next());
var sheet = spreadsheet.getSheets()[0];
var data = sheet.getRange("A1");
// Searches for your updater email and retrieves the message.
var findlabel = GmailApp.getUserLabelByName('AutoResponderUpdate');
var thread = findlabel.getThreads()[0];
//Checks if an update email was found.
if (thread != undefined){
var threadId = thread.getId();
var message = thread.getMessages()[0];
//Copies the data from your email and pastes it into a spreadsheet, then deletes the email entirely.
var content = message.getPlainBody();
var writeData = data.setValue(content);
Gmail.Users.Threads.remove(myemail, threadId);
} else {
//Reads data from spreadsheet if no email messages are found.
var readData = data.getValue();
var content = readData;
}
} else {
//Creates the spreadsheet that will hold your day off values if one is not found.
SpreadsheetApp.create('AutoResponder Data');
autoResponder();
}
// Specifies which days you have off. Sun=0 Mon=1 Tue=2 Wed=3 Thurs=4 Fri=5 Sat=6
//var daysOff = [content.toString().replace("char(10)", "")];
//var daysOff = [5,6];
var test = content.split(",");
var test2 = test.length;
var output = "";
if (test2 > -1){
for (var n = 0; n < test2; n++){
if (n === (test2 - 1)){
output = output + parseInt(test[n]);
} else {
output = output + parseInt(test[n]) + ",";
}
}
}
var daysOff = [output];
Logger.log(daysOff);
/* There is some code after this to auto reply to the email,
but I've left that out. */
}
当你说这不工作,我推测,你的意思是'Logger.log(daysOff)'日志什么,如果你是从电子邮件或电子表格中获取值。在日志中记录“message”,“content”,“readData”,“test”,“test2”和“output”的值是什么?您是否能够通过从电子表格中获取值来使您的代码正常工作? –
@DeanRansevycz所以'Logger.log(daysOff)'显示正确的数据。例如,如果我手动将'daysOff'设置为'[5,6]',日志将显示'[5.0,6.0]',但如果我从电子邮件或电子表格单元格中获取值'5,6',它将记录为'[5,6]'。从这个角度来看,我认为它像一个字符串而不是数字,但我不知道为什么或如何改变它。希望澄清它。 –
仍然不理解你的问题。在电子邮件或电子表格填充时,您在休息日发布自动回复的逻辑是不是在解释'daysOff'中的值?顺便说一句,为什么'output'是一个字符串,而不是一个数组?你的自动回复代码是否需要一个数组? –