2017-09-16 67 views
0

我創建了一個腳本,該腳本會自動響應在休息日發送給我的電子郵件。我最近決定對其進行升級,以便通過發送特殊格式的電子郵件,將數據從正文中提取出來,將這些數據存儲在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. */ 
} 
+0

當你說這不工作,我推測,你的意思是'Logger.log(daysOff)'日誌什麼,如果你是從電子郵件或電子表格中獲取值。在日誌中記錄「message」,「content」,「readData」,「test」,「test2」和「output」的值是什麼?您是否能夠通過從電子表格中獲取值來使您的代碼正常工作? –

+0

@DeanRansevycz所以'Logger.log(daysOff)'顯示正確的數據。例如,如果我手動將'daysOff'設置爲'[5,6]',日誌將顯示'[5.0,6.0]',但如果我從電子郵件或電子表格單元格中獲取值'5,6',它將記錄爲'[5,6]'。從這個角度來看,我認爲它像一個字符串而不是數字,但我不知道爲什麼或如何改變它。希望澄清它。 –

+0

仍然不理解你的問題。在電子郵件或電子表格填充時,您在休息日發佈自動回覆的邏輯是不是在解釋'daysOff'中的值?順便說一句,爲什麼'output'是一個字符串,而不是一個數組?你的自動回覆代碼是否需要一個數組? –

回答

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); 

上面應該產生數的數組值您正在尋找

+0

這是我尋找的編碼的神奇線,謝謝! –