Rephrasing a previous post for better understanding-
I have a JSON API that I am importing into Google Sheets. However, the columns are not consistently in the same order each time it reloads, which is common for JSON data. The issue arises when I try to append this data onto another sheet for storage; every other append results in mixed-up columns (e.g., column G becomes column F). This complicates the deduplication process because the system sees columns in different positions as unique entries.
Therefore, my queries are:
- Is there a way to use the script below to organize the columns in the same order (A:G) every time?
- Can I ensure that the API imports the columns into consistent positions despite changes at the source?
- Is it possible to reorganize or sort the column headers after appending them, similar to sorting an entire column but only affecting the header row?
The following is the script used to append data to sheet 2, which functions correctly:
function saveData() {
var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = spreadsheet.getSheets()[0];
var sheet2 = spreadsheet.getSheets()[1];
var rows = sheet1.getRange("A1:G" + sheet1.getLastRow()).getValues();
sheet2.getRange(sheet2.getLastRow()+1,1,rows.length,rows[0].length).setValues(rows); }