Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update Inventory Apps Script #18

Open
aadibhat09 opened this issue Nov 8, 2024 · 0 comments
Open

Update Inventory Apps Script #18

aadibhat09 opened this issue Nov 8, 2024 · 0 comments

Comments

@aadibhat09
Copy link
Member

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant