Looking for assistance in reorganizing columns in Google Sheets while appending data

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:

  1. Is there a way to use the script below to organize the columns in the same order (A:G) every time?
  2. Can I ensure that the API imports the columns into consistent positions despite changes at the source?
  3. 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); }

Answer №1

After some experimentation, I discovered a technique that involves utilizing option 3 without the need for scripting by incorporating the following formula directly into the worksheet.

=transpose(sort(transpose(A1:G),1,))

Although my initial preference was to accomplish this task entirely through scripting, I am content with this alternative method as well.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

show tab focus outline only

In search of a straightforward and effective method for focusable elements to display an outline only when the tab key is pressed, without showing it when using a mouse in React applications. (looking for something similar to :focus-visible that function ...

Tips for sending arrays to a webservice using asp.net

I'm encountering an issue while trying to pass an array as a parameter in ajax. I'm receiving an error regarding deserialization. Can someone please assist me in understanding how to correctly pass arrays to a web service? Error {"Message":"Typ ...

`Searching for a specific value within a nested JSON array: Tips and tricks`

How can I select only the sections that contain a "photo" within "employee" which is nested within "employees" using SQL in SNOWFLAKE? If a section does not have a "photo", I do not want it to be selected. For instance, consider the following JSON: { ...

Emailer: Missing Salutation

While attempting to send emails using Node with Nodemailer (https://github.com/nodemailer/nodemailer), the sendMail call from the Nodemailer transporter is throwing an error message of Greeting never received when connected to an Ethereal test email accoun ...

Unable to proceed, WISTIA upload frozen at 100% complete

I recently integrated the Wistia API for video uploads. Everything seemed to be working fine as per the Wistia documentation until I encountered an issue. After uploading a video file, the progress bar would reach 100%, but then an error was logged in the ...

Javascript encountered an error upon attempting to return from the main function

I have a function that calls the database to perform an action: function callQuery(query) { db.query(query, (err, res) => { if (err) { // Error connecting to DB console.log(err.stack) } else { // Return the results ret ...

Struggling to locate __proto__ of the global object known as "window."

Currently I am using Google Chrome browser. console.log(window.__proto__.__proto__.__proto__); console.log(window.__proto__.__proto__.__proto__ === EventTarget.prototype); The first code mentioned above returns EventTarget.prototype for me. This can be ...

Converting XML Schema into a JSON array list using Biztalk

Here is an example of a defined XML schema: <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:vc="http://www.w3.org/2007/XMLSchema-versioning" vc:minVersion="1.1"> <xs:element name="Root ...

The option list in AngularJS is cleared when an option is selected

In my current project, I am developing a django-tastypie api application with angularjs as the JavaScript framework. The main part of this application involves managing curriculum objects, each containing a list of grade objects and each grade object furth ...

What is the best way to ensure my button displays the complete description of a single country after showcasing all the countries?

I have designed a search feature for countries and I am looking to add a button next to the country names that will trigger one of my components responsible for displaying detailed information about the country. Currently, the details are shown only when t ...

The initial dispatch of a React hook triggers subsequent dispatches to fire multiple times

Whenever I include more than one dispatch in useEffect(), it causes all subsequent actions to be triggered twice. Please see the example below. Below is the complete code. Feel free to remove comments in any order you prefer. PS: I am not sure how to mak ...

Can ag-grid in react allow for the application of several filters on one column simultaneously?

I would like to personalize the agSetColumnFilter and incorporate the agNumberColumnFilter feature into it. What steps should I take to make this happen? ...

What are the steps to connect to multiple databases with ExpressJS?

I have a server with 3 databases containing identical tables. The databases are named DB1, DB2, and DB3. When working with a specific database, I utilize the following code in app.js: var cnxDB = require('./routes/cnxDB'); app.post('/user ...

fluctuating random percentage in JavaScript/jQuery

I am currently faced with the challenge of selecting a random number based on a given percentage ranging from 0 to 5. 0 - 25% (25/100) 1 - 25% (25/100) 2 - 20% (20/100) 3 - 15% (15/100) 4 - 10% (10/100) 5 - 5% (5/100) However, there are instances where ...

Encountering difficulty importing a class into the index.js file

225/5000 Hello, I seem to be facing a bit of trouble despite my efforts in trying to import a class into my index.js file. Here is what my index.js file looks like: import {Brandade} from "./modules/Brandade"; const brandade = new Brandade(&ap ...

Exploring the possibilities of JavaScript within the IntelliJ REST client

I've delved into the extensive documentation provided by JetBrains on their HTTP Client and how to incorporate requests using files with a .http extension. The challenge I'm facing involves utilizing a function from a separate .js file within on ...

Obtain Relative URL with the help of Selenium using the PhantomJS driver

Utilizing Selenium along with the Phantom JS driver, I am attempting to load an HTML page and extract all of the HREF links from it. The issue arises when PhantomJS provides absolute URLs after resolving them entirely. My specific requirement is to extrac ...

Jquery AJAX request encountering a syntax error due to an invalid label

I'm having trouble with an ajax call to the server that should return a JSON object. I can't seem to figure out what's wrong, so any help would be greatly appreciated. Here's the snippet of code: $.ajax ({ type : "GET", ...

The error message being thrown is: "Cannot access the 'top' property of an undefined value with the offset().top-500 method."

As a beginner in testing, I encountered a console error on my page while scrolling. The error message displayed was: main.js:18 Uncaught TypeError: Cannot read property 'top' of undefined at main.js:18 at dispatch (jquery-1.9.js:3) at v.han ...

My string is being cut off due to the HTML value

My webpage utilizes HTML5 to enable file uploads directly from the browser. The uploaded file is a PDF that needs to be displayed within an <input type="text"/> The following is my code snippet: var files = evt.target.files; // FileList object // ...