GAS:你可以打破下一个细胞吗?

时间:2013-01-30 15:25:36

标签: google-apps-script

我一直在修改一个脚本,让Facebook喜欢获得Facebook的见解。此脚本按城市获取用户的唯一页面展示次数,并将其全部显示在一个单元格中。

问题: 有没有办法将其分解成多个单元格?我尝试在表格上添加一行,但它告诉我我没有权限

代码:

////
// Facebook Fans Counter
// Made by Martin Hassman, http://twitter.com/hassmanm
// Released as Public Domain
// Look at http://labs.met.cz/ for other tools
// Edit by Jared Beach
//

function FacebookListOf(aPageId,aToken,aParameter)
{
  if (aPageId === undefined || aPageId === null)
  {
    throw "No parameter specified. Write Facebook PageID as parameter."
  }
  if (aToken === undefined || aToken === null)
  {
    throw "No parameter specified. Write Facebook Access Token as parameter."
  }
  if (aParameter === undefined || aParameter === null)
  {
    throw "No parameter specified. Write Facebook Access Token as parameter."
  }

  var today = new Date();
  today.setDate(today.getDate()-6);
  var dd = today.getDate();
  var mm = today.getMonth()+1; //January is 0!

  var yyyy = today.getFullYear();
  if(dd<10){dd='0'+dd} if(mm<10){mm='0'+mm} today = yyyy+'-'+mm+'-'+dd;

  if (typeof aPageId != "number")
    throw "Parameter must be number.";

  // See http://developers.facebook.com/docs/reference/fql/page/ for API documentation
  var url = "https://api.facebook.com/method/fql.query?query=SELECT%20metric,%20value%20FROM%20insights%20WHERE%20object_id=%22"+ encodeURIComponent(aPageId) + "%22%20AND%20metric=%27"+ encodeURIComponent(aParameter) + "%27%20AND%20end_time=end_time_date%28%27"+ encodeURIComponent(today) +"%27%29%20AND%20period=period%28%27day%27%29%20&%20access_token=" + encodeURIComponent(aToken);
  var response = UrlFetchApp.fetch(url);

  if (response.getResponseCode() != 200)
    throw "Unexpected response code from Facebook.";

  var responseText = response.getContentText();

  if (responseText == null || responseText == "")
    throw "Empty response from Facebook.";

  var value;

  try
  {
    var xml = Xml.parse(responseText, false);
    var page = xml.getElement().getElement();

    if (page == null)
      throw "Wrong PageID.";
    value = page.getElement("value").getElements("insMap");
    /*
    var format2 = value.toXmlString();
    return format2;
    */
    if (!value || value.length ==0) {
      throw "no match found";
    }
  }
  catch (e)
  {
    throw "Problem with response from Facebook: " + e;
  }

  var r ="";
  var listLength = value.length;

  for (var i = 0; i < listLength; i ++){
    r = r + value[i].getAttribute("key").getValue() +  " : " + value[i].getText() + "\n";
  }
  return r;

}

**结果截图:** Current Output

2 个答案:

答案 0 :(得分:2)

要在电子表格中编写一列,您需要像这样的[[],[],[],[],[]]二维数组 所以你可以修改脚本中的循环,如下所示:

  ...
  var r =[];
  var listLength = value.length;

  for (var i = 0; i < listLength; i ++){
    r.push([value[i].getAttribute("key").getValue() +  " : " + value[i].getText()]);
  }
  return r;

然后在你的SS中“打印”它,像这样使用setValues()(在屏幕截图中的第2行,第6列)

var sh = SpreadsheetApp.getActiveSheet();
sh.getRange(2,6,r.length,1).setValues(r);

编辑:我没有注意到你在哪里使用它作为自定义函数...所以忘记这个答案的最后部分。也就是说,它会对你如何使用该函数产生一些副作用,因为每个函数调用将占用多行(取决于你在结果中得到多少项)。
如果在连续行上使用它,数据将重叠......,只需在工作表设计中考虑这一点。

我用这个样本测试了你的函数:

function testMultiRow() {
  var r =new Array()
  var value=['row1','row2','row3','row4','row5','row6']
  var listLength = value.length;
  for (var i = 0; i < listLength; i ++){
    r.push([value[i]]);
  }
return r
}

enter image description here

答案 1 :(得分:0)

您可以使用标准电子表格功能来控制脚本自定义功能,例如:

= TRANSPOSE(SPLIT(YOUR_CUSTOM_FUNCTION),CHAR(10)&amp; CHAR(13)))