GAS: Merging Data from Various Google Sheets Spreadsheets in Google Drive to Create a Single Consolidated Workbook

I am looking to automate the process of reading multiple worksheets stored in a folder and appending specific data from columns D and H into a master file. Similar to how Google Forms captures responses on a Google Sheet.

Additionally, I want to move the read files into another folder after processing them. I have written some code for this purpose but facing some challenges. Any assistance with:

(a) Combining column data (D and H) into a single row on the master file. (b) Moving the copied files to a different folder once processed.

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  let combinedata = [];
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      combinedata.concat(data);
    }
  }
  ss.getRange(2,1,1,combinedata.length).setValues([combinedata]);
}

function getDataFromSpreadsheet(){
  var ss =  SpreadsheetApp.openById(ssID);
  var ws = ss.getSheets()[0]
  var data =  ws.getRange("A1:I" + ws.getLastRow()).getValues();
  data = data.reduce((acc,row) => acc.concat([row[3], row[7]]), []);
  return data;
}

Answer №1

Retrieving Specific Columns:

To obtain specific column values and store them in a single array, you can utilize the reduce function. This array can then be incorporated into your spreadsheet row as follows:

function getDataFromSpreadsheet(ssID){
  var ss =  SpreadsheetApp.openById(ssID);
  var ws = ss.getSheets()[0]
  var data =  ws.getRange("A1:I" + ws.getLastRow()).getValues();// how do I get the columns I want here (D & H)
  data = data.reduce((acc,row) => acc.concat([row[3], row[7]]), []);
  return data;
}

Subsequently, you will need to append this data to your primary sheet. Considering potential variations in the length of data from different spreadsheets, it is recommended to use appendRow within the loop rather than calling setValues just once outside. This approach accommodates differing lengths by appending rows individually:

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      ss.appendRow(data);
    }
  }
}

In cases where all columns are uniform in length, an alternative method entails:

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  let combinedata = [];
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      combinedata.push(data);
    }
  }
  ss.getRange(2,1,combinedata.length, combinedata[0].length).setValues(combinedata);
}

Lastly, for cases where data from multiple sheets should be combined into a single row, consider using this more streamlined approach:

function combinesheets() {
  const folder = DriveApp.getFolderById(Copy_Folder_id)
  const filesIterator = folder.getFiles();
  const ws = SpreadsheetApp.openById(Stage_sheet_id);
  const ss = ws.getSheetByName("Project List");
  let combinedata = [];
  while(filesIterator.hasNext()){
    file = filesIterator.next();
    fileType = file.getMimeType();
    if(fileType === "application/vnd.google-apps.spreadsheet"){
      ssID = file.getId();
      const data = getDataFromSpreadsheet(ssID);
      combinedata.concat(data);
    }
  }
  ss.getRange(2,1,1,combinedata.length).setValues([combinedata]);
}

File Transfer Process:

When relocating files to another folder, access the respective file and target folder via DriveApp, then execute the following command:

const file = DriveApp.getFileById("FILE_ID");
const folder = DriveApp.getFolderById("FOLDER_ID");
file.moveTo(folder);

Answer №2

You have a couple of queries.

Guide on relocating a document in Google Drive

To move a file in Google Drive, you will require the FileID and the FolderID to which you want to shift it. Both IDs can be found in the URL while navigating through Google Drive's interface.

https://i.sstatic.net/6kAlT.png

Here is a handy function that will help you execute this task:

function changeLocation(folderToMoveToID,fileID) {
  var destinationFolder = DriveApp.getFolderById(folderToMoveToID);
  var theFileToMove = DriveApp.getFileById(fileID);
  theFileToMove.moveTo(destinationFolder);
}

I will address your other question at a later time.

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

Establishing a connection to a JSON API to retrieve and process

I am trying to extract all instances of names from this page: . For guidance, I have been using this tutorial as a reference: . However, when I run the code provided, it doesn't return anything. What could be going wrong? var request = new XMLHttpRe ...

Nothing is being sent in the Ajax request to PHP

I am experiencing an issue with my AJAX post to PHP as the PHP code is indicating that the post is empty. Strangely, there are no error messages displayed by PHP or in the console. The alert(data) statement at the end of the code retrieves all the HTML con ...

Magento is prompting for admin login in response 302 after executing an AJAX call

I am facing an issue with my AJAX request in Magento block which triggers an action in my module controller. The problem is that the response of this request is a 302 status code, leading to a redirect to the admin sign-in page if I am not logged in. Howev ...

How can I remove unnecessary components from an API result in discord.js before sending it?

The particular API being discussed is a pun-themed one, specifically this one. Here is the code snippet I am currently utilizing: const superagent = require("superagent") module.exports = { run: async(client, message, args) => { const pun = a ...

What tips can you offer for beginning a Java program that utilizes arrays, classes, and methods to effectively store and manage student information?

I'm struggling with how to approach this particular situation. Can anyone provide some guidance on where to begin and maybe even offer a bit of structure? While I understand arrays, methods, and classes, I am unsure of how to start and what should be ...

What is the best way to create constants for accessing elements in a Ruby array?

I'm using an array to store field values and I use constants as element identifiers for easy addition and access. In the past, I manually set these constants like so: stages = ["lidnummer","aardf","prest","dt_prest","aantal","bedrag","verstrekker"," ...

Using a NextJS application alongside a pre-existing Node.js backend: A guide

I currently have a Node.js backend integrated with my create-react-app project. Now, I am in the process of transitioning to NextJS and have a few queries. My current authentication setup involves a simple JWT process: The frontend sends a request to my- ...

Ways to deactivate dates that come after today's date

Having a bit of trouble with my datepickers in two different views. I have written code to disable dates after the current date, and it works for the first view but not the second. This code snippet works: var yesterday = new Date(); yesterday.setTime(ye ...

Setting linked files to the executable directory during Qt deployment in C++

I'm currently developing a touchscreen app using Qt on win32, and I'm facing an issue with setting the URL of my linked file to the executable directory. (Similar to using "./file") QDir::currentpath doesn't seem to be working for me, as it ...

Iterating through a boolean array using the enhanced for loop in Java

My challenge involves dealing with a boolean array boolean[] test = { false, true, true, false, true }; I have attempted to toggle the values (true to false, and false to true) using a "for-each" statement like so: for(boolean x : te ...

`Erase content from a field once text is typed into a different field`

I have a Currency Converter with two input fields and a button. I enter the amount to be converted in the first field, and the result of the conversion appears in the second field. My question is: How can I automatically clear the second field when I type ...

Receive just a single notification for multiple operations using Ajax

In my Rails application, I am trying to fetch the status of each selected device. Currently, after executing the code, an alert "Successfully created execution record" pops up for every selected device. Instead, I want to display this alert only once at ...

Exploring the relationship among Node.js, TypeScript, JavaScript, and Angular

Sorry if this question has been asked before - I haven't come across a satisfactory answer yet. My Inquiry How are Node.js and TypeScript connected? What about TypeScript and Angular? I realize that TypeScript is a language that enhances Javascript& ...

Can someone explain the distinction between 'return item' and 'return true' when it comes to JavaScript array methods?

Forgive me for any errors in my query, as I am not very experienced in asking questions. I have encountered the following two scenarios :- const comment = comments.find(function (comment) { if (comment.id === 823423) { return t ...

The present user who is currently logged into the stackmob platform

I am currently working on retrieving a list of contacts for the logged-in user, but I am struggling to identify the current user. In Parse.com, you would use Parse.User.current(), but I am unsure if Stackmob offers a similar functionality. Below is the co ...

Utilizing React JS to call a static function within another static function when an HTML button is clicked

Can you please analyze the following code snippet: var ResultComponent = React.createClass({ getInitialState: function () { // … Some initial state setup ……. }, handleClick: function(event) { // … Handling click event logic …… // Including ...

Efficiently transforming an OpenMaya MPoint array into an imath V3f array in Python

I am faced with the challenge of connecting two APIs that have different data format requirements and I am looking for a quick solution to convert large arrays of one type into the other using Python instead of C++. One API (Maya) provides points in the f ...

Leveraging JavaScript to attach/append a query parameter to a URL

Currently, I am working with the DNN CMS platform and utilizing a module called ActionForm provided by DNNSharp to generate forms. Although there is an option in this module to display a form in a popup, I am facing a challenge in passing a query string t ...

Having trouble getting CSS styles to work properly in conjunction with Javascript code

Currently, I am developing a feature on a canvas that covers the entire webpage's width and length. In this canvas, I have the ability to create boxes by clicking down anywhere on the canvas, dragging my mouse in any direction, and upon releasing the ...

Out of nowhere, Div became completely unresponsive

As part of my lesson plan on jQuery, I wanted to demonstrate a sliding effect using a div element. Initially, the sliding worked fine when clicked, but then I tried adding code for hiding another element and encountered issues. Even after removing the new ...