AppsScriptPulse

New Feature for the AppSheetApp Library: Run API Calls in Parallel

A Google Apps Script wrapper for the Google AppSheet API – bienlim/AppSheetApp

Recently I had a nice ping into my LinkedIn feed from App Developer Bien Lim. Bien has enhanced my original AppSheetApp library for Apps Script with a powerful new feature: fetchAll(). This addition lets you run multiple AppSheet API calls at the same time, significantly boosting the performance of your Apps Script integrations.

Building on the library’s existing methods like Add, Delete, Edit, and Find, this new function allows developers to execute these calls in parallel. This can drastically reduce execution time, which is a big improvement for applications that rely on multiple API operations.

You can find the updated library on the code repository to start using this new feature today.

Example Code

Here is a snippet showing how you can use the new fetchAll() method:

/**
 * Executes multiple AppSheet API requests in parallel.
 * This example shows how to perform Add, Delete, Edit, and Find operations simultaneously.
 */
function parallelRequest(){
  // Replace with your actual App ID and Access Key
  const AppSheet = new AppSheetApp('YOUR_APP_ID', 'YOUR_ACCESS_KEY');

  // Placeholder data for demonstration
  const properties = { "Locale": "en-US" };

  // Sample data for adding a new record. The key field is usually omitted if it's auto-generated.
  const dataToAdd = [{"Name": "John Doe", "Age": 30}];

  // Sample data for editing an existing record. The key field is required to identify the row.
  const dataToEdit = [{"ID": "unique-id-123", "Age": 31}];

  // Sample data for deleting an existing record. The key field is required.
  const dataToDelete = [{"ID": "unique-id-456"}];

  // The FetchAll method takes multiple API calls as arguments.
  // The 'true' argument tells each method to return a parameter object instead of
  // making an immediate API call. These parameter objects are then passed to fetchAll().
  const responses = AppSheet.fetchAll(
    AppSheet.Add('People', dataToAdd, properties, true),
    AppSheet.Delete('People', dataToDelete, properties, true),
    AppSheet.Edit('People', dataToEdit, properties, true),
    AppSheet.Find('People', [], properties, true)
  );

  // The responses are returned in an array, in the same order as the requests.
  const [ respFromAdd, respFromDelete, respFromEdit, respFromFind ] = responses;

  // You can now handle each response individually
  console.log('Add Response:', respFromAdd);
  console.log('Delete Response:', respFromDelete);
  console.log('Edit Response:', respFromEdit);
  console.log('Find Response:', respFromFind);
}

Source: GitHub – bienlim/AppSheetApp: A Google Apps Script wrapper for the Google AppSheet API

Leave a Reply

Your email address will not be published. Required fields are marked *