2

I'm quite beginner with Apps Script / Javascript. I have some JSON data that I'm exporting using an API.

Below is what I get when I do: console.log(jsonData);

JSON data sample

I'm trying to:

  • Transform this JSON data into CSV.
  • Put the data in the first sheet of my active spreadsheet

Basically it should look like this:

Google Sheet output

However I'm stuck at this step and I cannot get the next piece of code right ... (how to transform into CSV and putting the data in a Google sheet).

Below is the beginning of my script:

function myFunction() {
  
  var apiKey = "MyApiKEY";
  var now = new Date();
  var startDate = new Date(now.setDate(now.getDate()-2))
  var endDate = startDate

  var startDateFormatted = Utilities.formatDate(new Date(startDate), "GMT+7", "yyyy-MM-dd");
  var endDateFormatted = Utilities.formatDate(new Date(endDate), "GMT+7", "yyyy-MM-dd");

  var url = 'https://public-api.vendor.com/v1/clicks?start_date='+ startDateFormatted + '&end_date=' + endDateFormatted;
  var options1 = {
    "method": "get",
    "headers": {
      "accept": "application/json",
      "Authorization": apiKey
    }
   }
  var response = UrlFetchApp.fetch(url, options1);
  var jsonData = JSON.parse(response.getContentText());

  // console.log(jsonData);

}

I would appreciate any help in getting those data into a Google Sheet. Also feel free to modify anything in the beginning of my script if you think something is wrong or unnecessary.

Damien
  • 143
  • 1
  • 10

1 Answers1

3

I believe your goal is as follows.

  • You want to convert your sample data to CSV data.
  • You want to put your sample data on the 1st sheet of the active Spreadsheet.

From your showing sample data, if jsonData is your showing sample data, how about the following modification?

From:

var jsonData = JSON.parse(response.getContentText());

To:

var jsonData = JSON.parse(response.getContentText());

// I added the below script.
const headers = ["date_of_report", "vendor_name", "product_name", "category", "avg_cpc", "avg_position", "clicks", "conversions", "conversion_rate", "cost", "cpl", "channel", "country", "email", "vendor_id"];
const values = [headers, ...jsonData.data.map(e => headers.map(h => e[h] || ""))];
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
sheet.getRange(1, 1, values.length, values[0].length).setValues(values);

// If you want to retrieve the values as CSV data. You can use the following script.
const csv = values.map(r => r.join(",")).join("\n");
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • Hi @Tanaike. The script works perfectly, this is great and thanks a lot for this! I just have one question: Do you have any documentation that could explain me how this line work? `const values = [headers, ...jsonData.data.map(e => headers.map(h => e[h] || ""))];` I'm learning Apps Scripts and I'm not sure how to reproduce this for a different case in the future. Again thanks a lot – Damien Aug 24 '22 at 06:33
  • 1
    @Damien Thank you for replying. I'm glad your issue was resolved. About `const values = [headers, ...jsonData.data.map(e => headers.map(h => e[h] || ""))]`, in this case, the data of each row is retrieved using the header value, and the header row is added to the top of array. By this, the header and data are put on the Spreadsheet. – Tanaike Aug 24 '22 at 06:40
  • Sorry I just realised the script displays the message: `Logging output too large. Truncating output.` So my Google Sheet can only display 100 rows at a time. According to this post https://stackoverflow.com/questions/65444065/google-apps-script-json-to-spreadsheet-result-too-long-api-to-sheet-work-on I need to logging the output inside a loop and print all the elements. Do you know how to do this on my script? Sorry for the disturbance – Damien Aug 24 '22 at 08:21
  • @Damien About your new question, I would like to support you. But the issue of replying is new issue, and that is different from your question. So can you post it as new question? Because when your initial question is changed by comment, other users who see your question are confused. By posting it as new question, users including me can think of it. If you can cooperate to resolve your new issue, I'm glad. Can you cooperate to resolve your new question? – Tanaike Aug 24 '22 at 08:25
  • Sure let me create a new question on Stackoverflow – Damien Aug 24 '22 at 08:35
  • If anyone is interested in my issue, here is the link: https://stackoverflow.com/questions/73470130/apps-script-how-to-solve-a-truncated-output-from-the-console – Damien Aug 24 '22 at 08:49