简体   繁体   中英

Is there a way to set a range to get data from all the sheets in a single spreadsheet?

I'm able to get values from a single sheet using code like you see below, but I was wondering if I can somehow get all the values from all the sheets in a Spreadsheet and then manipulate the date regarding on what I get. In other words, can you set the range to include sheets from sheet x to sheet y, or at least get all the data from all the sheets, and after that I'll somehow try to include/ exclude sheets. I'm using the Sheets API, and I can't use the Google App Scripts for this.

let { google } = require("googleapis");
let authentication = require("./authentication");

function getData(auth) {
  var sheets = google.sheets('v4');
  sheets.spreadsheets.values.get({
    auth: auth,
    spreadsheetId: '1XOqjUJ1eAMhl2g4KLIS4qPUzdwebSVeoE8OGJtPYyPw',
    range: 'Test!A2:C', **//I would love to get all sheets. This is only one of them. Possible?**
  }, (err, res) => {
    if (err) {
      console.log('The API returned an error: ' + err);
      return res;
    } 
    var rows = res.values;
    if (rows.length === 0) {
      console.log('No data found.');
    } else {
        for (var i = 0; i<= rows.length; i++) {
            var row = rows[i];
            console.log(row);
          }
        }
      });
    }

authentication.authenticate()
    .then((auth) => {
        getData(auth)
        .catch((err) => {
            console.log(err)
        });
});

This is the right way of doing this. I'm answering for potential future references.

copyFormatToRange(sheet, column, columnEnd, row, rowEnd)
Copy the formatting of the range to the given location. If the destination is larger or smaller than the source range then the source is repeated or truncated accordingly. Note that this method copies the formatting only.

const ss = SpreadsheetApp.getActiveSpreadsheet();
const source = ss.getSheets()[0];
const destination = ss.getSheets()[1];

const range = source.getRange("B2:D4");

// This copies the formatting in B2:D4 in the source sheet to
// D4:F6 in the second sheet
range.copyFormatToRange(destination, 4, 6, 4, 6);

Authorization Scripts that use this method require authorization with one or more of the following scopes :

- https://www.googleapis.com/auth/spreadsheets.currentonly
- https://www.googleapis.com/auth/spreadsheets

Here is the whole article . Good luck with it :)

You may refer with this blog on how to combine data from multiple sheets using query formula. Below is a sample Query formula to combine the above two sheets' data into a single sheet.

=query({junesheet!A2:H5;julysheet!A2:H5},”Select * where Col1 is not null “)

Here are additional links which might help:

Hope this helps!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM