So I wrote this code and it is not working as it should, it is pulling data from woocommerce Webhook with a “code.gs” code in GoogleSheets.
Problem is, if var product_name = myData.line_items[1].name; (and [2], [3] and [4]…. and others) does not exist, the code does not work in GoogleSheets…
What i would like to achieve is, when i have two products in an order (myData.line_items[1].name exists, myData.line_items[2].name exists,…) that GoogleSheets would make a new line with that data for each one of the products.
function doGet(e) {
return HtmlService.createHtmlOutput("request received");
}
function doPost(e) {
var myData = JSON.parse([e.postData.contents]);
var order_number = myData.number;
var order_created = myData.date_created;
var product_name = myData.line_items[0].name;
var product_qty = myData.line_items[0].quantity;
var product_total = myData.line_items[0].total;
var produktsku = myData.line_items[0].sku;
var product_name = myData.line_items[1].name;
var product_qty = myData.line_items[1].quantity;
var product_total = myData.line_items[1].total;
var produktsku = myData.line_items[1].sku;
var product_namea = myData.line_items[2].name;
var product_qtya = myData.line_items[2].quantity;
var product_totala = myData.line_items[2].total;
var produktskua = myData.line_items[2].sku;
var product_nameb = myData.line_items[3].name;
var product_qtyb = myData.line_items[3].quantity;
var product_totalb = myData.line_items[3].total;
var produktskub = myData.line_items[3].sku;
var product_namec = myData.line_items[4].name;
var product_qtyc = myData.line_items[4].quantity;
var product_totalc = myData.line_items[4].total;
var produktskuc = myData.line_items[4].sku;
var product_named = myData.line_items[5].name;
var product_qtyd = myData.line_items[5].quantity;
var product_totald = myData.line_items[5].total;
var produktskud = myData.line_items[5].sku;
var order_total = myData.total;
var billing_email = myData.billing.email;
var billing_first_name = myData.billing.first_name;
var billing_last_name = myData.billing.last_name;
var billing_countryshort = myData.billing.country;
var payment_method = myData.payment_method_title;
var shipping_method = myData.shipping_lines[0].method_title;
var shipping_total = myData.shipping_lines[0].total;
var shipping_total = myData.shipping_lines[0].total;
var klingi = "1";
var timestamp = new Date();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([timestamp,order_created,order_number,product_name,produktsku,product_qty,product_total,order_total,billing_email,billing_first_name,billing_last_name,payment_method,shipping_method,shipping_total,billing_countryshort]);
if( produktskua ) {
sheet.appendRow(["Izdelek 2", "",order_number,product_namea,produktskua,product_qtya,product_totala]);
};
if( produktskub ) {
sheet.appendRow(["Izdelek 3", "",order_number,product_nameb,produktskub,product_qtyb,product_totalb]);
};
if( produktskuc ) {
sheet.appendRow(["Izdelek 4", "",order_number,product_namec,produktskuc,product_qtyc,product_totalc]);
};
}
Any ideas?
It stops working, even if I wrap it, it works only if value exists…
if( myData.line_items[1].name ) {
var product_namea = myData.line_items[1].name;
var product_qtya = myData.line_items[1].quantity;
var product_totala = myData.line_items[1].total;
var produktskua = myData.line_items[1].sku;
};
2
Answers
Is solved like this:
When assigning your post data to variables, you can use the ternary operator
This allows you to verify either a certain postData exists, and if not – assign an empty string to the variable in order to prevent problems with Google Sheets.
Syntax:
Sample: