Google Apps脚本问题将数字解析为变量

时间:2017-09-16 21:15:58

标签: google-apps-script

我创建了一个脚本,可以自动响应我休假时发送给我的电子邮件。我最近决定对其进行升级,以便我可以通过向自己发送特殊格式的电子邮件来更新休息日,从正文中提取数据,将数据存储在Google表格中,然后提取该值并将其用作我的休假值。

我几乎所有东西都按照我需要的方式工作,但是当我从单元格读取的值传递给变量时,它不起作用。但是,当我在代码中将其设置为静态数字时,它可以正常工作。我认为这是因为它被读作一个字符串所以我把它分开并将它们专门用于数字。然而,这也没有奏效。所以现在我被卡住了。任何帮助,将不胜感激。

这是我的代码:

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. */
}

1 个答案:

答案 0 :(得分:1)

它表现得像一个字符串,因为你是concatenating字符串+数字:

output = output + parseInt(test[n])

导致字符串而不是数字。

var test = content.split(",");
var test2 = test.length;
var output = [];

test.forEach(function(e) { output.push(parseInt(e)) });

var daysOff = output;
Logger.log(daysOff); 

上面应该生成您正在寻找的数值值数组