Retrieve information from multiple tabs within a spreadsheet using Google Apps Script

I need some assistance with extracting data from multiple sheets in a spreadsheet using JavaScript. I have been encountering errors during my attempts, as I am not very familiar with JS. If anyone can provide guidance, it would be greatly appreciated.

function extractData(sheet){

    const ss = SpreadsheetApp.getActiveSpreadsheet();
    const ws = ss.getSheetByName(sheet);
    const data = ws.getRange("A1").getDataRegion().getValues();
    const headers = data.shift();
    const jsonArray = data.map(row =>{
      let obj = {};
      headers.forEach((header, index) => {
        obj[header] = row[index];
      });
      return obj;
  });
  const result = {'sheets': jsonArray}; 
}

function executeExtraction(){

  var sheetNames = ['check1', 'check2', 'check3', 'check4', 'check5'];

  for (var i=0; i<sheetNames.length; i++) {
    allData = extractData(console.log(sheetNames[i]));
  }
  return ContentService.createTextOutput(JSON.stringify(allData))
  .setMimeType(ContentService.MimeType.JSON);
}

executeExtraction();

Answer №1

Retrieve data from multiple sheets within a Google Sheets document

function fetchAllSheetData() {
  const spreadsheet = SpreadsheetApp.getActive();
  let dataObject = {};
  spreadsheet.getSheets().forEach(sheet => {
    dataObject[sheet.getName()] = sheet.getDataRange().getValues();
  });
  //Logger.log(JSON.stringify(dataObject));
  return dataObject;
}

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

How do I utilize ng-repeat in Angular to pass a variable that specifies the number of repetitions?

When working on my app, I encountered a situation where I needed to retrieve elements from a database and display them using ng-reat. Everything was going smoothly until I realized that within this list, there was another set of data that required a separa ...

Illuminating individual table cells as a user drags their mouse across a row

My goal is to create a feature that allows users to highlight cells in a table by dragging the mouse over them, similar to what is discussed in the question and answer provided here. However, I would like to limit this drag/highlight effect to only span w ...

How to apply a CSS style to a specific class based on conditions in Vue.js

Many questions have been asked about conditionally adding classes or styles to elements, but I want to take it a step further and conditionally style a class. In my application, users can customize foreground and background colors using a theming system. ...

What are some effective ways to utilize a marquee?

I am looking for a way to duplicate the <a> tag within a DIV named box so that the text is displayed on a continuous line marquee without any breaks. As I am new here, please feel free to ask for more information in the comments. Thank you. $(&apo ...

Using Array.push to add an object retrieved from a Redis cache in a Node.js application is causing issues and is not functioning as expected

I've encountered a problem with retrieving all keys from my Redis cache, storing them in an array, and sending that array to the user using Express. The issue arises when I receive an empty array as the response with no objects in it. I attempted to u ...

Monitoring the inclusion of a new item within the database

I am utilizing a Firebase Realtime Database where users have the ability to perform the following actions: add an item to the database update an item in the database Currently, I have a function that monitors a specific location within the database. ...

Receiving an error while trying to install packages from the NPM registry due to non

I am facing some challenges while attempting to install my Ionic App through the registry along with its dependencies. I have been using npm i --loglevel verbose command, and my ~/.npmrc file is configured as follows: //nexus.OMMITED.com/repository/:_auth ...

Error: undefined callback function in asynchronous parallel execution

Encountering the error message "callback is not defined" while attempting to utilize async.parallel has left me puzzled. Most examples of async.parallel demonstrate functions being inline, as shown in the async documentation here: https://caolan.github.io/ ...

Query for string type data between specific dates in MongoDB

I have my data stored in this format on MongoDB: { "_id" : { "$oid" : "5385a437084ea4734b03374f" }, "linea" : 1, "egunak" : [ { "fetxa" : "2014/05/26", "turnoak" : [ { ...

How can you use CakePHP3 to incorporate user notifications in a style similar to Facebook or a stack format through view cells?

When using platforms like Facebook or StackOverflow, we often receive notifications on the top navigation bar without refreshing the webpage. These are known as push notifications. I have a functional CakePHP 3 web app and my client wants to incorporate t ...

Error: Unable to encode data into JSON format encountered while using Firebase serverless functions

I am currently working on deploying an API for my application. However, when using the following code snippet, I encountered an unhandled error stating "Error: Data cannot be encoded in JSON." const functions = require("firebase-functions"); const axios = ...

The typical number of keys in a ChartJS graph is 2

Currently, I am utilizing ChartJS for a project that displays user-added records such as weight and heart rate for tracking purposes. I have encountered an issue where if a user adds two slightly different value records for the same day, it results in both ...

To view the previous or next image, simply click on the links and watch as the new image fades in seamlessly while maintaining

Whenever I click on a prev/next link, a specific div loads different images that loop. The JavaScript successfully changes the image source when the prev or next button is clicked. It works flawlessly. However, I am facing an issue. I want each new image ...

Choose a list in order to create a new list

If I were to choose a specific state in Nigeria, what steps should I follow to generate a drop-down menu with a list of local governments within that state? ...

The issue of the selection option not being cleared after being set to 0 persists in JavaScript

I am facing an issue where I need to reset the select option to `0` when another option is selected. I have tried the code below for this purpose. if (varALPO1MobNum == "0") { var selectElement = $(&apo ...

Is it possible to establish a delay for requestAnimationFrame()?

My webpage has a very long layout with text in one column and a floating element in another. I want the floating element to follow the scroll of the window and return to its original offset once scrolling stops. The current code I have is: var ticking = ...

Deciphering a JSON Array in JavaScript to extract specific components

I have a brief snippet for a JSON array and a JavaScript function that currently returns a single argument: <!DOCTYPE html> <html> <body> <h2>JSON Array Test</h2> <p id="outputid"></p> <script> var arrayi ...

Contradictory jQuery extensions

After exploring this site as part of my web coding self-study, I decided to register so I could ask a specific question. Currently, I am in the process of developing my new website and ran into an issue on a test page (www.owenprescott.com/home.html). The ...

Utilizing get requests to manage data in Node.js with MongoDB database integration

I'm a newbie and I have a question.. How do I synchronize the mongoose query with the JSON response? The current code is returning empty values because the query finishes executing later. What would be the best way to handle this issue? router.route( ...

The onPress function seems to be malfunctioning within the menu interface specifically on iOS devices

I am experiencing an issue where a problem only occurs on iOS devices, but Android works fine. My menu has a userMenu class. import Menu, {MenuItem, MenuDivider} from 'react-native-material-menu'; class UserMenu extends React.Component { ...