Script erro: numero de colunas diferentes

20 views
Skip to first unread message

jems 1983

unread,
Sep 9, 2023, 10:12:37 PM9/9/23
to Google Apps Script Community
Boa noite, tenho o script abaixo, preciso de ajuda com ele
function importDataFromJSON() {
  // Substitua o URL JSON e o token pelos seus dados reais

 
  var jsonURL = "https://graph.facebook.com/v17.0/" // versão do graph api
  +"act_xxxxxxxxx" // id conta de anúncio
  +"/insights" // início
  +"?fields=date_start,campaign_name,reach,impressions,frequency,spend,cpm,inline_link_clicks,cost_per_inline_link_click,inline_link_click_ctr,clicks,ctr,actions"
  +"&filtering=%5B%7Bfield%3A'action_type'%2Coperator%3A'IN'%2Cvalue%3A%5B'page_engagement'%5D%7D%5D" //filtro
  +"&period=day"
  +"&date_preset=this_month"
  +"&time_increment=1"
  //+"&limit=500"
  +"&level=ad"
  +"&access_token="+"meuToken";
 
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("test-fb"); // Change "Sheet1" to your sheet name
 
  var allData = [];
  var nextPageURL = jsonURL;
 
  while (nextPageURL) {
    // Buscar dados do URL JSON
    var response = UrlFetchApp.fetch(nextPageURL);
    var jsonData = JSON.parse(response.getContentText());
    var data = jsonData.data;
    nextPageURL = jsonData.paging ? jsonData.paging.next : null;
   
    allData = allData.concat(data);
  }
 
  // Grave os dados na planilha começando na linha 2
  var startRow = 2;
  var numRows = allData.length;
  var numCols = Object.keys(allData[0]).length;
  var range = sheet.getRange(startRow, 1, numRows, numCols);
 
  var values = [];
  allData.forEach(function (row) {
    var rowData = [];
    for (var key in row) {
      rowData.push(row[key]);
    }
    values.push(rowData);
  });
 
  range.setValues(values);
}
Porém no final da esse erro: The number of columns in the data does not match the number of columns in the range. The data has 11 but the range has 14.

Laurie Nason

unread,
Sep 10, 2023, 1:20:10 AM9/10/23
to google-apps-sc...@googlegroups.com
I think I would probably switch the order that the script runs as follows:

// Grave os dados na planilha começando na linha 2
  var startRow = 2;
 
  var values = [];
  allData.forEach(function (row) {
    var rowData = [];
    for (var key in row) {
      rowData.push(row[key]);
    }
    values.push(rowData);
  });
  var numRows = rowData.length;
  var numCols = rowData[0]).length;
  sheet.getRange(startRow, 1, numRows, numCols).setValues(rowData);





--
You received this message because you are subscribed to the Google Groups "Google Apps Script Community" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-apps-script-c...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-apps-script-community/d2828a35-9eeb-4d2c-82e8-791e1188dc92n%40googlegroups.com.


--

Laurie


Reply all
Reply to author
Forward
0 new messages