Unable to Consolidate Data from Multiple Sheets While Preserving Original Order (Google Sheets + Apps Script)

Recently, I encountered a situation where I was required to consolidate data from multiple sheets in Google Sheets while preserving the order based on the source sheet. Essentially, the objective was to append new data to existing entries while grouping data from each source sheet together.

The specific scenario involved working with several sheets such as Sheet1, Sheet2, Sheet3, and Sheet4. Each of these sheets featured a column for data entries along with a timestamp column indicating when the data was added. On a daily basis, fresh data would be appended to these sheets, and my task was to stack this information in a combined union sheet.

To illustrate, you can refer to this Picture.

The main goal was to organize the data in such a way that entries from the same source sheet remained intact and any new data was added below the existing records. The query was whether this could be achieved through Google Apps Script or any other techniques available. I would greatly appreciate any advice, guidance, or code snippets provided.

Thank you in advance for your help!

As an attempt to address this issue, I created a Google Apps Script function designed to extract data from each source sheet and consolidate it in a separate union sheet. However, the script encountered challenges in maintaining the order based on the original source sheet.

Below is an excerpt from the code:

function myFunction() {
  function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var destSheet = ss.getSheetByName("Approver");
  var sourceSheetNames = ["Assessor 1", "Assessor 2", "Assessor 3", "Assessor 4", "Assessor 5", "Assessor 6"];

  var newData = [];
  var prevData = [];
  
  sourceSheetNames.forEach(function(sheetName) {
    var sourceSheet = ss.getSheetByName(sheetName);
    var lastRow = sourceSheet.getLastRow();
    var lastColumn = sourceSheet.getLastColumn();
    var dataRange = sourceSheet.getRange(2, 19, lastRow - 1, 30);
    var dataValues = dataRange.getValues();
    
    // Separate data into newData and prevData based on criteria (e.g., day change)
    for (var i = 0; i < dataValues.length; i++) {
      if (dataValues[i][0] === "AB") {
        newData.push(dataValues[i]);
      } else if (dataValues[i][0] === "A") {
        prevData.push(dataValues[i]);
      }
    }
  });

Answer №1

Solution:

If your data includes timestamps, you can optimize the process by extracting information from other sheets, organizing it based on timestamps, and then updating the master sheet accordingly.

Try out this code snippet:

function updateMasterSheet() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var destSheet = ss.getSheetByName("Approver");
  var sourceSheetNames = ["Assessor 1", "Assessor 2", "Assessor 3", "Assessor 4", "Assessor 5", "Assessor 6"];

  var sheetData = [];

  sourceSheetNames.forEach(sheetName => {
    var sourceSheet = ss.getSheetByName(sheetName);
    var lastRow = sourceSheet.getLastRow();
    var lastColumn = sourceSheet.getLastColumn();
    var dataRange = sourceSheet.getRange(2, 1, lastRow - 1, lastColumn); //Adjust the range as needed
    var dataValues = dataRange.getValues();
    sheetData.push(dataValues);
  });

  var newData = sheetData.flat().sort((a, b) => a[1] - b[1]); //Sorts data based on date. Adjust the index as needed.
  var destRange = destSheet.getRange(2, 1, newData.length, destSheet.getLastColumn()); //Adjust the range as needed
  destRange.clearContent().setValues(newData);
}

Example data from all 6 Assessor sheets

https://i.sstatic.net/bguCD.png

Expected output on the Approver sheet

https://i.sstatic.net/Oa0QY.png

Additional Resources:

Answer №2

If you're looking for a solution, place this formula into cell A2 on the "Approver" sheet:

=query({query({Sheet1!A2:B},"select Col1, Col2 where Col1 is not null");query({Sheet2!A2:B},"select Col1, Col2 where Col1 is not null");query({Sheet3!A2:B},"select Col1, Col2 where Col1 is not null");query({Sheet4!A2:B},"select Col1, Col2 where Col1 is not null")},"select Col1, Col2")


EXAMPLE

https://i.sstatic.net/eMIA4.jpg

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

What is the best way to implement a new search input for my datatable while also enabling the searchHighlight feature?

I'm attempting to implement a search bar for my datatables. I need to hide the default search engine that comes with datatables, so I added a script that I found in a forum. However, when I try to run it in my code, it doesn't work and displays a ...

Is there a different option than jQuery.ajaxSettings.traditional for use with XMLHttpRequest?

Is there a different option similar to jQuery.ajaxSettings.traditional for xmlhttprequest? or What is the method to serialize query parameters for an xmlhttprequest? ...

What is the process for verifying the password field in bootstrap?

In my asp.net project using bootstrap, I have implemented password field validation. When the user clicks on the password field, an information box is displayed. <div id="pswd_info"> <h4>Password requirements:</h4> <ul> ...

What is the best way to control the class name of elements using jQuery in an MVC4 application?

Among the <li> elements, one of them contains the class "current". Determining which <li> is the current one. View below: @{ int k = 10; //the value changes with each request } <ul class="car"> @foreach (var item in modelCoun ...

Search in a table without the need to press the enter button on your keyboard

As a new react developer, I am working with an antd table that has search functionality. Is there a way to enable the search without having to press the 'enter' button on the keyboard? For example, when a user starts typing 'd..', the s ...

How come event handlers/listeners don't seem to work for elements I create using jQuery?

I'm currently working on improving my jQuery skills by creating a simple to-do list. I've run into an issue where event handlers aren't activating for elements I create in jQuery, but they work just fine for elements I've created in my ...

Substitute the identifier with an element for a specific webpage

Currently, my navigation consists of two list items: <body id="trips"> <nav> <li><a href="/trips.php" class="trips">Trips</a></li> <li><a href="/trips.php?offer=true" class="offers">Offers< ...

I'm struggling to figure out why Mongoose keeps throwing this undefined error in Object.isPOJO. Can you lend me a

I'm seeking assistance in understanding an error that mongoose has generated for me. Error Log: TypeError: Cannot read properties of undefined (reading 'name') at Object.isPOJO (C:\Users\[...]\node_modules\mongoose&b ...

Is there a way to use nightwatch.js to scan an entire page for broken images?

Hi there, I'm currently working on creating a test to ensure that all images are loaded on a webpage with just one single test. I assumed this would be a straightforward task that many people have already done before, but unfortunately, I haven' ...

A step-by-step guide on resolving the number type coerce error 50035 that occurs during command registration

I am delving into the world of creating a discord bot, and it has been a while since my last project using slash commands. However, I encountered an issue while trying to register my commands: DiscordAPIError[50035]: Invalid Form Body application_id[NUMBER ...

What is the best way to access a nested promise element in Protractor?

I've recently put together a function in protractor that I'd like to share: function findChildElementByText(parentElement, tagName, textToSearch) { return parentElement.all(by.tagName(tagName)) .then((items) => { items.map( item ...

Set up a JavaScript ArrayBuffer containing only zeros

Looking for an efficient way to quickly set all indices in an ArrayBuffer to 0. I am aware of manually iterating through it, but I want a faster built-in method since I need to perform this task once per animation frame. ...

Tips for delaying form submission until the xhmhttprequest finishes

I am working on a Flask Web App that utilizes Boostrap and plain JavaScript. The challenge I am facing is related to making an XMLHttpRequest to upload a file to s3 when a form is submitted. However, the issue arises because the page reloads before the xhr ...

Issue: Compilation error encountered when attempting to build module (from ./node_modules/@angular-devkit/build-angular/src/babel/webpack-loader.js):

I encountered an issue while trying to set up Angular Material. Initially, I received an error message. I attempted to resolve it by deleting the node modules and running npm i again, but the problem persists. How can I rectify this? Upon trying to launch ...

Absence of event firing in .on method in Asp.NET Core 6 when using JQuery

I am currently working on an ASP.NET Core 6 application that utilizes JQuery. Here is the JQuery code snippet I have implemented in my page: @section Scripts { @{ await Html.RenderPartialAsync("_ValidationScriptsPartial"); } ...

Restricting user access to a route based on its type to enhance security and control

Currently, I have a React, Redux, and Next.js app up and running. Within my redux store, there is a user object that contains an attribute called "type". Each type of user has its own set of "routes" they are allowed to access. I am looking for the most e ...

What is the best way to access a webpage that has been received through an ajax request

My ajaxcall is giving me back an html page in the response. I'm trying to figure out how to open this in a new window. Any ideas on how to make it happen? https://i.sstatic.net/xguL5.png The console.log(data) command seems to output all the html con ...

What is the best way to make a CSS element move with Javascript?

Currently working on a JavaScript game where I am in need of a CSS object to replace the original JavaScript object. Specifically, I want my "sword" CSS object to move along with my player object when it is Unsheathead. All the examples I find only show wh ...

Guide to displaying a complex object in an accordion component using React Native

My data structure is quite complex, structured like this: { "hot foods": [ { "name": "something", "ingredients": "something", "price": 13 }, { "name": "something", "ingredients": "something", "price": ...

Enhancing Grails dynamic dropdown to include a pre-selected value in edit.gsp

One feature I have implemented is a dynamic dropdown menu in my _form.gsp file that appears in both the create and edit pages. While it currently works as intended, I am seeking a way to display the selected value on the edit page. _form.gsp <g:s ...