Update Products Immediately From Google Sheets Script
While our existing templates allow for scheduled data pulls from Google Sheets whenever a row is created, this workflow caters to immediate updates. By setting up an Apps Script under the Extensions > Apps Script menu, this workflow sends a webhook to MESA every time a row is updated. Perfect for real-time inventory management and product updates, this solution ensures your Shopify store always reflects the latest data from your Google Sheets.
Topics:
Watch the demo
Example spreadsheets
The Apps Script code
function onEdit(e) {
var sheet = e.source.getActiveSheet();
var range = e.range;
var editedRow = range.getRow();
var headersRange = sheet.getRange(1, 1, 1, sheet.getLastColumn());
var headers = headersRange.getValues()[0];
var valuesInEditedRow = sheet.getRange(editedRow, 1, 1, sheet.getLastColumn()).getValues()[0];
// Create an object to store the key-value pairs
var rowData = {};
// Iterate through the headers and values to create the object
for (var i = 0; i < headers.length; i++) {
var header = headers[i];
var value = valuesInEditedRow[i];
rowData[header] = value;
}
postWebhook(rowData);
}
function postWebhook(payload) {
let jsonPayload = JSON.stringify(payload);
let webhookUrl = 'INSERT WEBHOOK URL HERE';
// Send the JSON payload to the webhook URL using a POST request
var options = {
method: "post",
contentType: "application/json",
headers: { 'Accept': '*/*' },
muteHttpExceptions: true,
accept: 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*; q = 0.8',
// muteHttpExceptions: true,
payload: jsonPayload
};
Logger.log("Payload: " + jsonPayload);
var response = UrlFetchApp.fetch(webhookUrl, options);
Logger.log(response.getContentText());
}
Preview workflow steps
Free download
This free workflow template if yours to personalize. Simply import the ZIP file to MESA then connect the app steps to your credentials. Test, then, turn it on!
Questions about this workflow demo? Contact:
Kalen Jordan
MESA Service Partner