Skip to content

Update Inventory Apps ScriptΒ #18

@aadibhat09

Description

@aadibhat09

Apps Script for toolkit backend updating

Code (updateInventory.gs)

function updateInventory() {
  
  // Get spreadsheet from ID in URL
  const spreadsheetID = '1Q050KV5nT77dEtTWGhSG6hyduQXwEvKc_TVKbMAB2xo';
  const spreadsheet = SpreadsheetApp.openById(spreadsheetID).getSheetByName('Sheet1');

  // Collect values from spreadsheet
  const range = spreadsheet.getDataRange();
  const values = range.getValues();
  const columns = values.shift();

  // Convert 2D array to map
  const data = values.map(row => {
    const rowObject = {};
    for (let i = 0; i < columns.length; i++) {
      rowObject[columns[i]] = row[i];
    }
    return rowObject;
  });

  // URL of deployed app
  const endpointURL = "https://optixtoolkit-backend-production-abcd.up.railway.app/";

  // Set up POST request for each item in the map
  data.forEach(item => {
    Logger.log(`Preparing to send: ${JSON.stringify(item)}`);
    const options = {
      method: "post",
      contentType: "application/json",
      payload: JSON.stringify({
        endpoint: "post-tool",
        name: item.name,
        category: item.category,
        reserverID: `${item.barcodeId}`,
      })
    };
   
    // Submit a POST request
    const response = UrlFetchApp.fetch(endpointURL, options);
    Logger.log(response.getContentText());
  });
}

// Call the function
function doGet(e) {
  updateInventory();
  return ContentService.createTextOutput("Inventory update completed.");
}

Purpose

  • This script helps to automate inventory updates for Toolkit to streamline data flow from Google Sheets to the backend
    • Data about build and electrical's tools stored on this spreadsheet
      • Name, barcode ID, category, and count
  • It is deployed on Google Apps Script

Explanation

The updateInventory() function does the following:

  • Retrieve data from Google Sheets
  • Sends data to the backend API endpoint at post-tool to update the inventory
  • Each item is sent as a POST request with details:
    • name
    • category
    • reserverID

Logging

A message is logged before and after a POST request is submitted for each item.

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions