SBG Posted Monday at 06:39 AM Posted Monday at 06:39 AM I have try a lot of different way to get data from Receipts by Item with google sheet script every column of my data went fine expect "Categoty Name" and "Modifier Applied (Option Name)" here is my sample code that's function function getLoyverseData_Fixed() { const ss = SpreadsheetApp.getActiveSpreadsheet(); let sheet = ss.getSheetByName(SHEET_NAME) || ss.insertSheet(SHEET_NAME); sheet.clear(); const headers = [ "Date", "Receipt #", "Item Name", "Category Name", "Modifier Applied (Option Name)", "Modifier ID", "Status" ]; sheet.appendRow(headers); const categoriesMap = fetchCategoriesMap(); const todayStart = new Date(); todayStart.setHours(0, 0, 0, 0); const url = `https://api.loyverse.com/v1.0/receipts?created_at_min=${todayStart.toISOString()}&limit=250`; const options = { 'headers': { 'Authorization': 'Bearer ' + LOYVERSE_TOKEN }, 'muteHttpExceptions': true }; const response = UrlFetchApp.fetch(url, options); const json = JSON.parse(response.getContentText()); if (!json.receipts) { Logger.log("Failed"); return; } let rows = []; json.receipts.forEach(r => { r.line_items.forEach(item => { let catName = item.category_name || categoriesMap[item.category_id] || "No Category"; let modOptions = []; let modIds = []; if (item.modifiers) { item.modifiers.forEach(m => { modOptions.push(m.option_name || m.name); // ดึง Option Name ตามภาพ modIds.push(m.modifier_id); }); } rows.push([ r.created_at, r.receipt_number, item.item_name, catName, modOptions.join(", ") || "-", // คอลัมน์ U modIds.join(", ") || "-", // คอลัมน์ V r.receipt_type ]); }); }); if (rows.length > 0) { sheet.getRange(2, 1, rows.length, headers.length).setValues(rows); sheet.autoResizeColumns(1, headers.length); Logger.log("Done checking at" + SHEET_NAME); } } function fetchCategoriesMap() { let catMap = {}; try { const res = UrlFetchApp.fetch('https://api.loyverse.com/v1.0/categories?limit=250', { 'headers': { 'Authorization': 'Bearer ' + LOYVERSE_TOKEN } }); const json = JSON.parse(res.getContentText()); if (json.categories) { json.categories.forEach(c => { catMap[c.id] = c.name; }); } } catch(e) { Logger.log("Failed"); } return catMap; } result is Category and Modifiers return blank and I can't use this data to run around powerBI with sheet sources to visualize for my team. By far I'm not sure if I call a wrong data name or I just can't get these both data. I'm not really developer just a marketing so if anyone can help me fixed this please.
Recommended Posts