-
Notifications
You must be signed in to change notification settings - Fork 2
Open
Description
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
- Data about build and electrical's tools stored on this spreadsheet
- 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-toolto update the inventory - Each item is sent as a POST request with details:
namecategoryreserverID
Logging
A message is logged before and after a POST request is submitted for each item.
Metadata
Metadata
Assignees
Labels
No labels
