如何在ArrayFormula中使用Google Script方法UrlFetchApp.fetch?

时间:2017-03-29 13:05:33

标签: google-sheets

function getContentForAPI(path) 
{
  var result = {};

  result.url = "https://" + HOST + "/rest/api/2/" + path;
  result.response = UrlFetchApp.fetch(result.url, API_HEADERS);
  result.text = result.response.getContentText();

  try { result.data = JSON.parse(result.text); } catch(error) {}

  return result;
}

我有一个像这样的自定义公式:

/**
 * Import data.
 *
 * @param {string} the ID that belongs to the issue.
 * @return The summary.
 * @customfunction
 */
function IMPORTDATA(issueID) 
{
  var details = [];

  if (issueID instanceof Array) {
    var issueIDs = issueID;
    for (i in issueIDs){
      for (j in issueIDs[i]){
        issueID = issueIDs[i][j];

        var content = getContentForAPI("issue/"+issueID);
        if (content == undefined || content.data == undefined) {
          details.push(["G","H"]);
        } else {
          details.push(["E", "F"]);
        }
      }
    }
  } else {
    var content = getContentForAPI("issue/"+issueID);
    if (content.data != undefined) {
      details.push([content.data.fields.project.name, content.data.fields.summary]);
    }
  }

  return details;
}

这适用于单个公式=IMPORTDATA(A2)但是当我将它放在一个ArrayFormula =ARRAYFORMULA(IMPORTDATA(A2:A))中时,结果是#ERROR但是当我注释掉UrlFetchApp.fetch()时,它的工作方式似乎错误似乎是来自那种方法。有谁知道为什么?

0 个答案:

没有答案