为什么.setValue()在for循环跳过列中?

时间:2019-12-17 17:16:12

标签: javascript google-apps-script google-sheets sirishortcuts

我在Appscript中有这个Web应用程序,该应用程序从Siri快捷方式的POST请求中接收信息。

function doPost(e) {
  
  //Get active spreadsheet and input database tab name.
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Test02');
  
  if(typeof e !== 'undefined')
    //Retrieve data received and make a string.
    var data = JSON.stringify(e);
  
    //1st print. Print string on new row.
    sheet.getRange(sheet.getLastRow()+1,1).setValue(data);
  
    //Eliminate all special characters coming from Siri Shortcuts POST request.
    data = data.replace(new RegExp(["\\\\"], 'g'), "");
    data = data.replace(new RegExp('"', 'g'), "");
    data = data.replace(new RegExp("{", 'g'), "");
    data = data.replace(new RegExp("}", 'g'), "");
    data = data.replace(new RegExp(":", 'g'), "");
  
    //Split all info on the left side of the first parameter and on the right side of the second.
    data = data.split('Log')[1];
    data = data.split("name")[0];

    //2nd print. Print clean string on a new row.    
    sheet.getRange(sheet.getLastRow()+1,1).setValue(data);  
     
    //3rd print. Print all array element on row. End goal is to have them start from column A and keep adding 1 column every loop.
    //Split string into array.
    var dataArray = data.split(",");
    var lastRow = sheet.getLastRow()+1;
    for(var i = 0; i < dataArray.length; i++) {
      //3rd print, first part. This tests the real data.
      sheet.getRange(lastRow,1+[i]).setValue(dataArray[i]);
      //3rd print, second part. This tests the index number to try and find why row are being skipped.
      sheet.getRange(lastRow+1,1+[i]).setValue([i]);
    }
  
    //Use Mail app to send mail after completion.  
    MailApp.sendEmail("abc123@gmail.com","InningLogger - Test", data); 
    
    return;
  
}

附件是我在电子表格中得到的结果。 Spreadsheet Result

第3行和第7行有两个问题:

  1. 为什么数据不是从A列开始?
  2. 为什么第7行跳过一吨 的列数(我将其隐藏为屏幕截图)以打印最后3列 值?

谢谢!

1 个答案:

答案 0 :(得分:0)

正如@Pointy所提到的,问题在于for循环中的1+ [i]。 “因为我在您的代码中是一个数字,所以说3,1+ [i]将是字符串“ 13”,而不是数字。将其更改为[i + 1]可以完全解决问题。