英文:
Reading Clover data into a Google Sheet
问题
以下是代码部分的中文翻译:
这个函数在填充电子表格时出现了以下错误:
TypeError: 无法读取未定义的属性(读取 'elements')
getCloverSales @ getCloverSales.gs:33
我似乎无法弄清楚如何捕获错误并继续获取记录的范围。
任何帮助将不胜感激。我对JavaScript还不熟悉,正在努力理解它。
在此先感谢任何见解。
请注意,这是您提供的代码的错误消息和相关描述的翻译。
英文:
The function below is populating the spreadsheet until it gets this error:
TypeError: Cannot read properties of undefined (reading 'elements')
getCloverSales @ getCloverSales.gs:33
I can't seem to figure out how to trap the error and continue to get the range of records.
Any help would be greatly appreciated. I'm a noob to javascript and I'm trying to wrap my brain around it.
Thank you in advance for any insight.
function getCloverSales(startDate, endDate) {
var startDate = "1675530917000"; // epoch unix format incl ms
var endDate = "1676177999000"; // epoch unix format incl ms
var API_KEY = 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx';
var MERCHANT_ID = 'xxxxxxxxxxxxx';
var url = 'https://api.clover.com/v3/merchants/' + MERCHANT_ID + '/orders?filter=createdTime%3E' + startDate + '&filter=createdTime%3C' + endDate + '&expand=lineItems&limit=1000&orderBy=createdTime';
var response = UrlFetchApp.fetch(url, {
headers: {
Authorization: 'Bearer ' + API_KEY
}
});
var data = JSON.parse(response.getContentText());
Logger.log(startDate);
Logger.log(endDate);
Logger.log(data);
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sales Custom');
sheet.clearContents();
sheet.appendRow(['Date', 'Item Code', 'Item Name', 'Price', 'Order Ref']); // Write the header row
for (var i = 0; i < data.elements.length; i++) {
Logger.log(i);
var order = data.elements[i];
var lineItems = order.lineItems.elements;
for (var j = 0; j < lineItems.length; j++) {
Logger.log(j);
Logger.log(lineItems);
var lineItem = lineItems[j];
var itemName = lineItem.name;
var itemCode = lineItem.itemCode;
var itemTime = lineItem.createdTime;
var date = new Date(itemTime);
var formattedDate = Utilities.formatDate(date, 'GMT-7:00', 'MM-dd-yyyy HH:mm:ss');
var price = lineItem.price / 100; // Convert from cents to dollars
// var total = lineItem.total;
var orderRef = lineItem.orderRef;
sheet.appendRow([formattedDate, itemCode, itemName, price, orderRef]); // Write the sales item data into the sheet
}
}
}
答案1
得分: 0
以下是翻译好的部分:
Modification points:
- 从您提供的示例值中,我可以找到当前问题的原因是
TypeError: Cannot read properties of undefined (reading 'elements')
。在您的值中,没有具有lineItems
属性的元素不存在。因此,似乎出现了这种错误。 - 在您的脚本中,
appendRow
在循环中使用。在这种情况下,处理成本将很高。参考(作者:我) - 我认为
var orderRef = lineItem.orderRef;
可能应该是var orderRef = lineItem.orderRef.id;
。
当这些要点反映在您的脚本中时,以下是如何修改的建议:
从:
sheet.appendRow(['Date', 'Item Code', 'Item Name', 'Price', 'Order Ref']); // Write the header row
for (var i = 0; i < data.elements.length; i++) {
Logger.log(i);
var order = data.elements[i];
var lineItems = order.lineItems.elements;
for (var j = 0; j < lineItems.length; j++) {
Logger.log(j);
Logger.log(lineItems);
var lineItem = lineItems[j];
var itemName = lineItem.name;
var itemCode = lineItem.itemCode;
var itemTime = lineItem.createdTime;
var date = new Date(itemTime);
var formattedDate = Utilities.formatDate(date, 'GMT-7:00', 'MM-dd-yyyy HH:mm:ss');
var price = lineItem.price / 100; // Convert from cents to dollars
// var total = lineItem.total;
var orderRef = lineItem.orderRef;
sheet.appendRow([formattedDate, itemCode, itemName, price, orderRef]); // Write the sales item data into the sheet
}
}
到:
var ar = [['Date', 'Item Code', 'Item Name', 'Price', 'Order Ref']];
for (var i = 0; i < data.elements.length; i++) {
if (!data.elements[i] || !data.elements[i].hasOwnProperty("lineItems") || !data.elements[i].lineItems.hasOwnProperty("elements")) continue;
var temp = [];
var order = data.elements[i];
var lineItems = order.lineItems.elements;
for (var j = 0; j < lineItems.length; j++) {
var lineItem = lineItems[j];
var itemName = lineItem.name;
var itemCode = lineItem.itemCode;
var itemTime = lineItem.createdTime;
var date = new Date(itemTime);
var formattedDate = Utilities.formatDate(date, 'GMT-7:00', 'MM-dd-yyyy HH:mm:ss');
var price = lineItem.price / 100;
var orderRef = lineItem.orderRef.id;
temp.push([formattedDate, itemCode, itemName, price, orderRef]);
}
ar = [...ar, ...temp];
}
sheet.getRange(1, 1, ar.length, ar[0].length).setValues(ar);
英文:
Modification points:
- From your provided sample value, I could find the reason for your current issue of
TypeError: Cannot read properties of undefined (reading 'elements')
. In your value, the element without the property oflineItems
not existing. By this, it seems that such the error occurs. - In your script,
appendRow
is used in a loop. In this csae, the process cost will be high. Ref (Author: me) - I thought that
var orderRef = lineItem.orderRef;
might bevar orderRef = lineItem.orderRef.id;
.
When these points are reflected in your script, how about the following modification?
From:
sheet.appendRow(['Date', 'Item Code', 'Item Name', 'Price', 'Order Ref']); // Write the header row
for (var i = 0; i < data.elements.length; i++) {
Logger.log(i);
var order = data.elements[i];
var lineItems = order.lineItems.elements;
for (var j = 0; j < lineItems.length; j++) {
Logger.log(j);
Logger.log(lineItems);
var lineItem = lineItems[j];
var itemName = lineItem.name;
var itemCode = lineItem.itemCode;
var itemTime = lineItem.createdTime;
var date = new Date(itemTime);
var formattedDate = Utilities.formatDate(date, 'GMT-7:00', 'MM-dd-yyyy HH:mm:ss');
var price = lineItem.price / 100; // Convert from cents to dollars
// var total = lineItem.total;
var orderRef = lineItem.orderRef;
sheet.appendRow([formattedDate, itemCode, itemName, price, orderRef]); // Write the sales item data into the sheet
}
}
To:
var ar = [['Date', 'Item Code', 'Item Name', 'Price', 'Order Ref']];
for (var i = 0; i < data.elements.length; i++) {
if (!data.elements[i] || !data.elements[i].hasOwnProperty("lineItems") || !data.elements[i].lineItems.hasOwnProperty("elements")) continue;
var temp = [];
var order = data.elements[i];
var lineItems = order.lineItems.elements;
for (var j = 0; j < lineItems.length; j++) {
var lineItem = lineItems[j];
var itemName = lineItem.name;
var itemCode = lineItem.itemCode;
var itemTime = lineItem.createdTime;
var date = new Date(itemTime);
var formattedDate = Utilities.formatDate(date, 'GMT-7:00', 'MM-dd-yyyy HH:mm:ss');
var price = lineItem.price / 100;
var orderRef = lineItem.orderRef.id;
temp.push([formattedDate, itemCode, itemName, price, orderRef]);
}
ar = [...ar, ...temp];
}
sheet.getRange(1, 1, ar.length, ar[0].length).setValues(ar);
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论