What is the best way to query from multiple Google Sheets in Google Scripts: index columns

KitzyKitt

Background:

I am creating a Google Script app which is used for approving/rejecting applications. I have three sheets which reference each other, like a database with foreign keys.

Sheets:

Application

(primary key)
appid           |   userid  |   startdate   |   submitteddate   |   firstname   |   lastname    | ...
  • This sheet has one row per applicant/userid (not all users will have applications)

Approvals

(primary key)       (foreign key Application)    (foreign key Admin)
approvalid      |   appid                       |   adminid         |   reviewernotes   |   approvalstatus  |   ...
  • This sheet has multiple rows per application/appid, different admins will weigh in with their thoughts/notes

Admin

(primary key)
adminid         |   adminemail  |   role
  • This sheet contains the roles of each admin

I'm going to be checking that an admin has specific roles to access/comment/approve or reject an application. Those comments are stored in the Approvals tab.

Currently, the admin info is setup as soon as they login to the app. There are multiple processes I'd like to do for them several get, add, and edit functions.

The Problem:

I know that the best practice is to limit calls to Google Sheets[1]. So I'm looking for the best way to access this data. Specifically, to do a call like getApprovalsForAdmin(adminid) which would return an object which combines data from both the Application and Approval tabs.

I know this could be done by:

function getApprovalsForAdmin(adminid)
{
  var approvals = [];
  var ss = SpreadsheetApp.openById (spreadsheet_id)
  var sheet = ss.getSheetByName ("Approvals");
  var values = sheet.getDataRange ().getValues ();
  values.forEach (function (value) {
    if (values [2] == adminid){
      approvals.push (value);
    }
  });

  var return_values = [];
  sheet = ss.getSheetByName ("Application");
  var applications = sheet.getDataRange ().getValues ();
  applications .forEach (function (application){
    approvals.forEach (function (approval){
      if (application [0] == approval [1]){
        return_values.push (createReturnValue (application, approval));
      }
    });
  });

  return (return_values);
}

This, however, does have two separate ss.getSheetByName(...) calls.

  • Is there a way to do this with only one call?

My Search for and Answer:

First

I have seen people make ONE sheet with all the data merged together, but I'm not sure this is the best solution because of how fluid people submitting either application or comments via approvals, is. A merged sheet would be updated frequently, and might cause more of a synchronization headache.

Second

The closest answer I have found: Using Google Scripts to Query Multiple Google Sheets to A View written back in July 2018.

The only solution given is that the person should use a traditional database. I'm trying to make this quickly, and while the data changes a lot, it isn't going to grow into a huge thing. This sounds like overkill in my situation.

Third

This title looked promising: Fetch values from multiple sheets Google sheets However, the solution involved making serveral calls to spreadsheet.getSheetByName("name") which is exactly what I'm wondering if could be simplified.

==========================

Footnotes

[1] https://developers.google.com/apps-script/guides/support/best-practices

Rubén

getRangeList(A1Notations) could be what you are looking for.

Returns the RangeList collection representing the ranges in the same sheet specified by a non-empty list of A1 notations or R1C1 notations.

Or Method: spreadsheets.values.batchGet but to use it you should use the Sheets Advanced Service

Related

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Multiple columns in QUERY with "and" in Google Sheets

What would be the best way to create multiple Google Apps Scripts for an organization?

Google Sheets Scripts relating to multiple columns

Google Sheets Index query for Multiple Sheets

Google Sheets - Index, Match formula from multiple columns

Google Sheets Query Sum Multiple Columns automatically

Google sheets Query and Importrange - selecting different columns from different sheets

Is there a way to send emails with google scripts (sheets) not from gmail

Multiple 2 columns in Google sheets that are fed from a google form

Is there a way to insert blank columns in output with google sheets query?

Error when trying to query from multiple sheets in Google Sheets

Is there a way to match cells across multiple columns/rows in google sheets?

Google Sheets Match/Index Query

Query multiple columns of data in Google Sheets and count number occurrences

Google Sheets QUERY with WHERE on multiple columns at the same time

Filtering multiple columns in Google Sheets

Google Sheets Search Multiple Columns

Multiple query filters on Google Sheets

Google Sheets Query multiple range

Google Sheets Query With Multiple IF Statements

Multiple Issues with Google Sheets Scripts (JavaScript)

Adding 2 computed columns to the results from a query in Google Sheets

How to select specific columns from array in Google Sheets query?

Lookup multiple columns in Google Sheets and return a list from the matches

Remove multiple columns from Google Sheets avoiding expensive loop

Copy values from multiple columns through via script - Google Sheets

Is it possible to stack Google Sheet columns from multiple sheets into one report?

What's the best way to unify date formats after retrieving them from cells and ranges in Google Sheets to Apps Script?

How to list or index all sheets/tabs in Google Sheets with formulas (no scripts)