簡易範例
輸出一個多層次複雜物件,包含字串、數字、巢狀物件與陣列
function demoObjectParsing() {
// 多層次複雜物件,包含字串、數字、巢狀物件與陣列
const orderData = {
orderId: "A-2026-9981",
totalAmount: 1700,
customer: {
name: "王小明",
email: "ming@example.com"
},
items: [
{ productName: "無線滑鼠", price: 500 },
{ productName: "機械鍵盤", price: 1200 }
]
};
// 直接輸出差異比較
// Logger.log("--- 直接輸出差異比較 ---");
Logger.log(orderData);
console.log(orderData);
// 使用 + 號串接文字
// Logger.log("--- 使用 + 號串接文字 ---");
Logger.log("訂單資料為: " + orderData);
console.log("訂單資料為: " + orderData);
// 使用 JSON.stringify 格式化
// 第一個參數是物件
// 第二個參數(取代函數)給 null
// 第三個參數給 2 (代表縮排 2 格)
Logger.log("--- JSON.stringify 格式化 ---");
Logger.log("訂單資料為: \n%s", JSON.stringify(orderData, null, 2));
console.log(`訂單資料為:
${JSON.stringify(orderData, null, 2)}`);
}
直接輸出差異比較
執行項目
這裡會有每次執行 Logger、console 紀錄的 log 資訊,但該 log 資訊並不會保留很久,官方文件 - Use the Apps Script execution log 說明為




沒有留言:
張貼留言