簡體   English   中英

Google Apps腳本:無法訪問返回對象的元素

[英]Google Apps Script: Unable to Access Elements of Returned Object

問題

無法僅訪問返回對象的編號。 得到逗號,似乎無法停止這種行為。

當前成果

    goalList = data[i][3].toString(); -> [2,10,3,4];
    var goal1 = goalList[0]; -> 2
    var goal2 = goalList[2]; -> 1
    var goal3 = goalList[4]; -> ,
    var goal4 = goalList[6]; -> ,

期望的結果

    goalList = data[i][3].toString(); -> [2,10,3,4];
var goal1 = goalList[0]; -> 2
var goal2 = goalList[2]; -> 10
var goal3 = goalList[4]; -> 3
var goal4 = goalList[6]; -> 4

工作流程

我有一個函數可以返回電子表格中的值。 getReportData()被傳遞一個包含4個元素的數組。

var profileList = getIds(processId);

if (profileList.length == 0) {
  processStatus = "Completed";
  ctrlRange.setValues([[processId, processStatus]]);
} else {
  getReportData(profileList, processId);
}
} catch(error) {
 Browser.msgBox(error.message);
 }
}
 function getIds(curPid) {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName('Filtered Views');
  var data = sheet.getDataRange().getValues();

  var profilecmsArray = []

  for (i = 0; i < data.length; i++) {
    var pid = data[i][3];
    if (pid != curPid) {
      profilecmsArray.push([data[i][1], data[i][2], i+1, data[i][4]]);
    }
  }  
  return profilecmsArray;
}

for (i = 0; i < data.length; i++) {
 profileId = data[i][0];
 clientCms = data[i][1];    
 goalList = data[i][3].toString();


 var goal1 = 'ga:goal' + newGoal[0] + 'Completions'
 var goal2 = 'ga:goal' + newGoal[2] + 'Completions'
 var goal3 = 'ga:goal' + newGoal[4] + 'Completions'
 var goal4 = 'ga:goal' + newGoal[6] + 'Completions'

 Logger.log(goal1);
 Logger.log(goal2);
 Logger.log(goal3);
 Logger.log(goal4);

您的代碼將數組轉換為字符串,而不是將每個元素轉換為單獨的字符串。 作為數組, [2,10,3,6]的第一個元素為2,然后為10,然后為3,然后為6。作為字符串, 2,10,3,6的第一個字符2然后,然后。 1然后是0 ,然后是3 ,然后是6

要更正,請嘗試以下操作:

for (i = 0; i < data.length; i++) {
 profileId = data[i][0];
 clientCms = data[i][1];    
 goalList = data[i][3];


 var goal1 = 'ga:goal' + goalList[0].toString() + 'Completions'
 var goal2 = 'ga:goal' + goalList[1].toString() + 'Completions'
 var goal3 = 'ga:goal' + goalList[2].toString() + 'Completions'
 var goal4 = 'ga:goal' + goalList[3].toString() + 'Completions'

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM