Utilizing Google Apps Script along with JavaScript code, I am leveraging UrlFetchApp to retrieve data from the Stripe API in order to fetch an invoice. Subsequently, the Script processes the retrieved data and updates a Google Sheet template with it.
An issue that I am currently facing is that when the API does not contain information for a specific field related to a customer, the script encounters errors. The API indicates this absence of data as NULL; for instance, "discount": null
might denote that a customer has no discount on the invoice.
Whenever the script encounters a line containing no data (NULL response), it breaks and ceases to execute further. My objective is to modify the behavior such that if there is no data available, the script should continue running and return a specified value denoting the absence of data (such as returning 0 for no discount) instead.
Here is the snippet of my code:
function getInvoiceObj()
{
var apiKey, content, options, response, secret, url;
secret = "rk_live_xxxxxxxxxxxxxxxxxx";
apiKey = "xxxxxxxxxxxxxxxxx";
url = "https://api.stripe.com/v1/invoices/in_xxxxxxxxxxxxx?expand[]=charge&expand[]=customer";
options = {
"method" : "GET",
"headers": {
"Authorization": "Bearer " + secret
},
"muteHttpExceptions": true
};
response = UrlFetchApp.fetch(url, options);
//Push data to Sheet from invoice. **Overwrites existing Sheet data**
content = JSON.parse(response.getContentText());
var sheet = SpreadsheetApp.getActiveSheet();
/* Customer Discount */
sheet.getRange(21, 2).setValue([content.discount.coupon.percent_off]);
}