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.

Cooper
  • 59,616
  • 6
  • 23
  • 54
MIXD_Toms
  • 25
  • 5

0 Answers0