Jared Beach
Jared Beach

Reputation: 3133

Can you line break to the next cell?

I have been modifying a script that gets Facebook likes to get Facebook insights instead. This script gets the user's unique page impressions by city and displays them all in one cell.

Question: Is there any way to break it up into multiple cells? I tried appending a row to the sheet, but it told me I didn't have permission

Code:

////
// 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;

}

**Screenshot Of Result: ** Current Output

Upvotes: 0

Views: 430

Answers (2)

David Tew
David Tew

Reputation: 1471

You can use standard spreadsheet functions to control your script custom function, such as:

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

Upvotes: 0

Serge insas
Serge insas

Reputation: 46794

to write a column in a spreadsheet you need a 2D array like this [[],[],[],[],[]] so you could probably modify the loop in your script like this :

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

And then to "print" it in your SS, use setValues() like this (in row2, column 6 as in your screen capture)

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

EDIT : I didn't notice you where using this as a custom function... so forget the last part of this answer. That said, it will have a few side effects on how you can use the function since each function call will occupy more than one row(depending on how many items you get in the result).
If you use it on consecutive rows data will be overlapping..., just take this into account in your sheet design.

I tested your function with this sample :

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

Upvotes: 3

Related Questions