`I have a scenario where I need to stack data from multiple sheets in Google Sheets while maintaining the order based on the source sheet. Essentially, I want to append new data to existing data while keeping the entries from each source sheet together.
Here's the scenario I'm dealing with:
I have multiple sheets (e.g., Sheet1, Sheet2, Sheet3, Sheet4). Each sheet contains a column for data entries and a separate timestamp column to indicate when the data was added. Each day, new data is appended to these sheets, and I want to stack this data in a separate union sheet. For instance, Picture
My goal is to stack the data while keeping entries from the same source sheet together and append new data below the existing data.
Is there a way to accomplish this using Google Apps Script or other techniques? Any guidance or code samples would be greatly appreciated.
Thank you in advance for your assistance!
I wrote a Google Apps Script function that extracted data from each source sheet and attempted to stack it in a separate union sheet. However, the script wasn't able to maintain the order of entries based on the source sheet. `
Here is my 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]);
}
}
});