我相信您的目标如下。
- 您想要将示例数据转换为 CSV 数据。
- 您希望将示例数据放在活动电子表格的第一张纸上。
从您显示的样本数据来看,如果jsonData
您显示的是示例数据,以下修改如何?
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");