写入Google表格之前,从输出中删除“未定义”

时间:2018-08-16 11:22:01

标签: google-apps-script google-sheets google-shopping-api

当我在要写入的数组中添加 dfStatus.error.count 时,下面的脚本向Google表格返回“未定义”。

function getDatafeedStatus() {
  var d = new Date();
  var ar = [];
  for (var a in FEEDS) {
    for (var i = 0; i < FEEDS[a].length; i++) {
      try {
        var dfStatus = ShoppingContent.Datafeedstatuses.get(a, FEEDS[a][i]);
        // see https://developers.google.com/shopping-content/v2/reference/v2/datafeedstatuses for detail on this API call
        ar.push([d, a, FEEDS[a][i], dfStatus.processingStatus, dfStatus.lastUploadDate, dfStatus.itemsValid, dfStatus.errors.count]);
      } catch (e) {
         Logger.log(e.message); // check View > Logs after running the script if a feed does not appear to be fetching correctly
      }
    }
  }
  appendArrayToSheet(ar, 'status');
}

/**
  * Add an array to the bottom of a sheet. If the sheet doesn't exist, it is created.
  * @param {array} ar - the array to write
  * @param {string} sheetName - the name of the sheet to which to write
  */
function appendArrayToSheet(ar, sheetName){
    if (ar.length !== 0 && ar[0].length !== 0){
      var ss =  SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName(sheetName);
      if (sheet == null) { 
        sheet = ss.insertSheet();
        sheet.setName(sheetName);
      };
      var range = sheet.getDataRange();
      var row = range.getLastRow() + 1;
      var newRange = sheet.getRange(row, 1, ar.length, ar[0].length);
      newRange.setValues(ar);
}

任何想法有什么问题吗?我必须说我不是JavaScript专家。我正在尝试修改其他地方已经使用的代码。

0 个答案:

没有答案