This is my APP Script code through which I'm trying to append a row to a sheet. It writes the data but then always returns err which triggers a .fail()
function is JavaScript:
function doGet(e) {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();;
var sheet = ss.getSheets()[0];
var name = e.parameter.name;
var address = e.parameter.address;
var phone = e.parameter.phone;
var email = e.parameter.email;
var city = e.parameter.city;
var quantity = e.parameter.quantity;
var productTitleFinal = e.parameter.productTitleFinal;
var productPriceFinal = e.parameter.productPriceFinal;
var fullDate = e.parameter.fullDate;
var comment = e.parameter.comment;
var url = e.parameter.url;
var rowData = sheet.appendRow([fullDate, name, email, phone, address, city, productPriceFinal, comment, productTitleFinal, quantity, url]);
var result = "Insertion successful";
return result
}
catch (err) {
return err
}
}
This is JavaScript code from which sends a request to write data
var script_url = "https://script.google.com/macros/s/AKfycbx5_btANVIwai5PJ1CT_VHocfcbIhy-_vViGSg6vzDfE_4hOFg4/exec";
var url = script_url+"?fullDate="+fullDate+"&name="+name+"&email="+email+"&phone="+phone+"&city="+city+"&address="+address+"&comment="+comment+"&quantity="+quantity+"&productTitleFinal="+productTitleFinal+"&productPriceFinal="+productPriceFinal+"&comment="+comment+"&url="+url;
var request = jQuery.ajax({
crossDomain: true,
url: url ,
method: "GET",
dataType: "jsonp",
})
.done(function(res){
console.log(res)
})
.fail(function(e){
console.log(e)
})
.always(function(e){
console.log(e)
});
What could be wrong with this code?
I think that the reason of your issue is that doGet()
always doesn't return the status 200. In order to avoid this issue, how about the following modification?
Please modify your script as follows.
return result
To:
return ContentService.createTextOutput(result); // Modified
var request = jQuery.ajax({
crossDomain: true,
url: url ,
method: "GET",
dataType: "jsonp",
})
To:
var request = jQuery.ajax({
crossDomain: true, // Also this might not be required.
url: url ,
method: "GET",
// dataType: "jsonp", // Modified
})
return err
, the error message is not returned. So if you want to return the error message, please modify to ContentService.createTextOutput(err)
. But in this case, the status code 200 is returned. So if you want to retrieve the error message as the error, it is required to prepare the script in the function of done(function(res){})
. Because at the Web Apps of Google Apps Script, the custom status code cannot be used.If I misunderstood your question and this was not the direction you want, I apologize.