0

I recently got a code to work, which would return the adspend of a Facebook ad account in a certain time frame in a Google Sheets cell. However, the returned data is not in the correct form/Google Sheets cant read it. What I mean by this, is that, even if the cell contains a number, using, for example, =SUM(X1;X10) it wouldn't interpret this as a number and would return 0. This is the code (excluding my access token):

function FacebookReporting(input1, input2) {
  var AD_ACCOUNT_ID = input1
  var TIME_RANGES = input2
  const LEVEL = 'account'
  const FIELDS = 'spend'
  const TOKEN = 'my_access_token'
  const facebookUrl = `https://graph.facebook.com/v14.0/act_${AD_ACCOUNT_ID}/insights?level=${LEVEL}&fields=${FIELDS}&time_ranges=${TIME_RANGES}&access_token=${TOKEN}&limit=1000`;
  const encodedFacebookUrl = encodeURI(facebookUrl);
  const options = {
    'method': 'post'
  };
  const fetchRequest = UrlFetchApp.fetch(encodedFacebookUrl);
  const results = JSON.parse(fetchRequest.getContentText());
  var data = [];
  results.data.forEach(function (pieceOfData) {
    data.push(pieceOfData.spend);
  });
  return data;
}

The code works perfectly, but summing the numbers is important for to calculate the total spend of a client, etc. The formula that is placed in the cell is

=FacebookReporting("598530075324846","[{since:'2022-08-01',until:'2022-08-30'}]")

Any help or suggestions are greatly appreciated :)

P.S. I tried to format the number from Google Sheets aswell, but it didnt change anything.

4
  • You need to show us the function that sums the data. This simply pushes a value into an array.
    – TheWizEd
    Aug 11, 2022 at 13:03
  • Summing the data is done in Google sheets, for example, using the =SUM(A1;A10) function that would sum the numbers in a column of 10.
    – MIXD_Toms
    Aug 11, 2022 at 16:53
  • Then how does this have anything to do with the script you've shown? And shouldn't your formula be =SUM(A1:A10)?
    – TheWizEd
    Aug 11, 2022 at 17:48
  • data.push(Number(pieceOfData.spend))
    – TheMaster
    Aug 11, 2022 at 19:10

0