0

我一直在修改一个让 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;

}

**结果截图:** 电流输出

4

2 回答 2

3

要在电子表格中写入一列,您需要一个像这样的二维数组,[[],[],[],[],[]] 因此您可以像这样修改脚本中的循环:

  ...
  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
}

在此处输入图像描述

于 2013-01-30T17:43:20.250 回答
0

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

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

于 2013-01-31T11:18:22.967 回答