I have a custom function that looks up the current number of likes for a specific facebook page. I also have a custom function, set to run once a day by a trigger,that is supposed to copy and paste the cells containing the facebook query function. This will allow me to have in two columns 1) date and 2) number of likes, updating for each new day. I have to copy and paste values before the day is out so that the number of likes for each day does not automatically recalculate to the current number of likes.
The problem I am having is between the copying and pasting of the formula and copying and pasting of the values. It appears the formula is not loading fast enough as I keep on pasting the value “thinking…”. I have tried the Utilities.Sleep() function to pause the script before copy and pasting values but it does not appear to solve the problem. Both custom functions are below. Any advice?
function FBlikes(url) {
var jsondata = UrlFetchApp.fetch("https://graph.facebook.com/"+url);
var object = Utilities.jsonParse(jsondata.getContentText());
return object.likes; //returns the number of "likes"
}
//pastes FB likes
function updateFbLikes() {
var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Like History");
var rangeToCopy = s.getRange("A2:E2"); //sets second line as the range to be copied
var range = s.setActiveCell("A331");
var notLastRow = true;
while(notLastRow){ //finds last row
if(range.getRow() == s.getLastRow()){
notLastRow = false;
s.insertRowAfter(s.getLastRow());
}
range = range.offset(1, 0);
}
rangeToCopy.copyTo(range); // Paste the data
range = range.offset(0, 0, 1, 5); //grab the whole new pasted row
Utilities.sleep(10000); //slowdown so data will load before pasting values
range.copyValuesToRange(s, range.getColumn(), range.getColumn()+4, range.getRow(), range.getRow()); //paste values
}
If I understand correctly and what you are trying to do is take the results from row 2 and place them at the end of the page as static values.
If so you might try:
function updateFbLikes() { var s = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Like History"); var valuesToCopy = s.getRange("A2:E2").getValues(); s.appendRow(valuesToCopy[0]); }If there is more to it please let me know.