var $website_routespace="pssg_namespace_route",$website_routespace_multiple="multiple_product_route",$website_wpjson="pssg_json_website",$pssg_access_key="undefined"!=typeof AppsScriptAccessKey?AppsScriptAccessKey:"";function onOpen(e){var t=SpreadsheetApp.getActiveSpreadsheet(),s=t.getActiveSheet(),r=s.getName();if(""===YourSheetTabName||YourSheetTabName===r){var o=s.getLastColumn();o<5&&(o=10);var a="Your Sheet is Connected";t.toast(a,"Information",5),Logger.log(a)}}function doEdit(e){var t=SpreadsheetApp.getActiveSpreadsheet(),s=t.getActiveSheet(),r=s.getName();if(""===YourSheetTabName||YourSheetTabName===r){if(!$pssg_access_key||""===$pssg_access_key){t.toast("Access Key is not configured. Please copy the new AppsScript from your WordPress plugin settings.","Error",10);return}var o=e.range,a=o.getValues();if(o.getNumRows()>1||o.getNumColumns()>1){var n=0;t.toast("Wait...","Please",6);for(var c=s.getRange(1,1,1,s.getLastColumn()).getValues()[0],g={},i=0;i<a.length;i++){for(var p=0;p<a[i].length;p++){var l=o.offset(i,p).getRow();o.offset(i,p).getColumn();for(var u=s.getRange(l,1,1,s.getLastColumn()).getValues()[0],d=u[0],f={},y=0;y<c.length;y++){var v=c[y];f[v=v.replace(/ /g,"_")]=u[y]}g[d]=f}n++}n>1?(t.toast("Multiple update started..","Updating...",6),sendToserver(g,$website_routespace_multiple)):1==n&&(t.toast(n,"Notice",16),singleCellEdit(e))}else singleCellEdit(e)}}function singleCellEdit(e){var t=SpreadsheetApp.getActiveSpreadsheet(),s=t.getActiveSheet(),r=s.getActiveCell();r.getValue();var o=r.getRow(),a=r.getColumn();if(1==o||1==a||2==a){t.toast("Unable to change product_id or product_type column.","Info",6);try{var n=e.oldValue;s.getRange(o,a).setValue(n);return}catch(c){return}}if(!$pssg_access_key||""===$pssg_access_key){t.toast("Access Key is not configured. Please copy the new AppsScript from your WordPress plugin settings.","Error",10);return}t.toast("Updating a Product data","Info",6);var g=s.getRange(o,1).getValue();s.getRange(o,2).getValue();for(var i=s.getRange(1,1,1,s.getLastColumn()).getValues()[0],p=s.getRange(o,1,1,s.getLastColumn()).getValues()[0],l={},u=0;u<i.length;u++){var d=i[u];l[d=d.replace(/ /g,"_")]=p[u]}Logger.log(l);var f=$website_wpjson+$website_routespace,y=!1;if(""==g&&3==a)y=!0,t.toast("Creating a new Product","Creating ....",5);else if(""==g)return;var v={method:"post",contentType:"application/json",redirect:"follow",payload:JSON.stringify(l),headers:{"X-PSSG-Access-Key":$pssg_access_key}};try{var _=UrlFetchApp.fetch(f,v);if(_.getResponseCode()>=200&&300>_.getResponseCode())y?t.toast("New Product Added","Information",5):t.toast("Product Update successful!","Information",5);else{var $=_.getContentText();try{var m=JSON.parse($);m.message?t.toast("Error: "+m.message,"Access Key Error",10):t.toast("Error: "+$,"Error",10)}catch(h){t.toast("Error: "+$,"Error",10)}}}catch(A){var C=A.message;-1!==C.indexOf("403")||-1!==C.indexOf("401")?t.toast("Access Key Error: Invalid or missing Access Key. Please copy the new AppsScript from your WordPress plugin settings.","Access Key Error",10):t.toast(f+"\n"+C,"DNS Error",10)}}function sendToserver(e,t){var s=SpreadsheetApp.getActiveSpreadsheet();if(!$pssg_access_key||""===$pssg_access_key){s.toast("Access Key is not configured. Please copy the new AppsScript from your WordPress plugin settings.","Error",10);return}var r={method:"post",contentType:"application/json",redirect:"follow",payload:JSON.stringify(e),headers:{"X-PSSG-Access-Key":$pssg_access_key}};Logger.log("started");try{var o=UrlFetchApp.fetch($website_wpjson+t,r);if(o.getResponseCode()>=200&&300>o.getResponseCode())s.toast("Multiple Update Success","Response Code: "+o.getResponseCode(),5);else{var a=o.getContentText();try{var n=JSON.parse(a);n.message?s.toast("Error: "+n.message,"Access Key Error",10):s.toast("Error: "+a,"Response Code: "+o.getResponseCode(),10)}catch(c){s.toast("Error: "+a,"Response Code: "+o.getResponseCode(),10)}}}catch(g){var i=g.message;-1!==i.indexOf("403")||-1!==i.indexOf("401")?s.toast("Access Key Error: Invalid or missing Access Key. Please copy the new AppsScript from your WordPress plugin settings.","Access Key Error",10):s.toast("DNS Error: \nYou are unable to upload to a localhost server.\n"+i,"DNS Error",10)}}