ต้องการสร้างฟีด XML จาก Google Sheet Table
ขอความช่วยเหลือหน่อยตอนนี้ฉันซ่อมแซมโค้ดเกือบทั้งวันแล้วและฉันก็ติดขัดและดูเหมือนว่าจะเป็นวิธีแก้ปัญหาที่ดีที่สุดที่ฉันพบจนถึงตอนนี้
ฉันกำลังพยายามสร้างสคริปต์ที่จะสร้างไฟล์ XML จากตาราง Google Sheet
แผ่นตัวอย่างเป็นดังนี้ >> https://docs.google.com/spreadsheets/d/1tSWMiXBRyhFcAmwpH5tEJiyHCB5vjlGwuHFv865-85A/edit?usp=sharing
ฉันเจอตัวอย่างโค้ดนี้Google Script Export Spreadsheet เป็นไฟล์ XMLและเป็นสิ่งที่ฉันต้องการ 90% และฉันทำให้มันใช้งานได้ผ่านการเผยแพร่เป็นเว็บแอปที่นี่ >>https://script.google.com/macros/s/AKfycbxVcUi6dXw0D1CWfZTlwf94gAT9QjqpG__-SaCIHVFVPzftndU/exec?id=1tSWMiXBRyhFcAmwpH5tEJiyHCB5vjlGwuHFv865-85A
ตอนนี้ฉันติดอยู่กับการทำให้มันวนรอบส่วนหัวและค่าเนื่องจากต้องจัดรูปแบบ XML
ฉันยังพบว่าค่าบางค่ามีแอตทริบิวต์ดังนั้นฉันจึงพบว่าเป็นการยากที่จะเพิ่ม xml: lang = "x-default" ในตัวอย่างด้านล่าง 10.00 น.: 18.00 น.
นี่คือตัวอย่างของสิ่งที่ฉันพยายามสร้าง
<store store-id="F123">
<name>Store One</name>
<address1>123 Street</address1>
<address2></address2>
<city>London</city>
<postal-code>L67 9JF</postal-code>
<phone>123 456</phone>
<store-hours xml:lang="x-default">10AM | 6PM</store-hours>
<custom-attribute attribute-id="freeTextTitle" xml:lang="x-default">Store Description Title</custom-attribute>
<custom-attribute attribute-id="v3_store_open_hours_0" xml:lang="x-default">11 AM|7 PM</custom-attribute>
</store>
<store store-id="G456">
<name>Store Two</name>
<address1>123 Street</address1>
<address2></address2>
<city>Manchester</city>
<postal-code>L67 9DS</postal-code>
<phone>123 456</phone>
<store-hours xml:lang="x-default">10AM | 6PM</store-hours>
<custom-attribute attribute-id="freeTextTitle" xml:lang="x-default">Store Description Title</custom-attribute>
<custom-attribute attribute-id="v3_store_open_hours_0" xml:lang="x-default">11 AM|7 PM</custom-attribute>
</store>
ขอบคุณมาก
** เพิ่มบริบทเพิ่มเติม
ขอบคุณทั้งคู่จริงๆแล้วฉันติดอยู่บนฟังก์ชันแผนที่ JavaScript () ในฟังก์ชัน doIt ที่พยายามแมปส่วนหัวและแอตทริบิวต์
function doGet(e) {
var content;
try {
content = doIt(e);
} catch(err) {
content = '<error>' + (err.message || err) + '</error>';
}
return ContentService.createTextOutput(content).setMimeType(ContentService.MimeType.XML);
}
function doIt(e) {
if (!e) throw 'you cannot run/debug this directly\nyou have to either call the url or mock a call';
if (!e.parameter.id) throw '"id" parameter not informed. Please provide a spreadsheet id.';
var values = SpreadsheetApp.openById(e.parameter.id).getSheets()[0].getRange('A1:J4').getValues();
return '<sheet>' + values.map(function(row, i) {
return '<row>' + row.map(function(v) {
return '<cell>' + v + '</cell>';
}).join('') + '</row>';
}).join('') + '</sheet>';
}
ค่าจะคว้าค่าทั้งหมดในช่วง แต่ฉันหลงทางเล็กน้อยในการพยายามทำลายค่าต่างๆ
ฉันอ่านบางส่วนบนฟังก์ชั่นแผนที่ () ป่วยจนต้องไปอีก
คำตอบ
เป็นการปรับเปลี่ยนอย่างง่ายการแก้ไขต่อไปนี้เป็นอย่างไร?
ในสคริปต์ของคุณ<sheet>
, <row>
และ<cell>
ได้ใช้ แต่ดูเหมือนว่าสิ่งเหล่านี้จะไม่รวมอยู่ในผลลัพธ์ที่คุณคาดหวัง เมื่อคุณต้องการใช้แถวส่วนหัวของแถวที่ 1 เป็นแต่ละแท็กจำเป็นต้องใช้ในสคริปต์ เมื่อสคริปต์ของคุณได้รับการแก้ไขจะเป็นดังนี้
สคริปต์ที่แก้ไข:
ในการแก้ไขนี้คุณdoIt()
ได้รับการแก้ไขแล้ว
function doIt(e) {
if (!e) throw 'you cannot run/debug this directly\nyou have to either call the url or mock a call';
if (!e.parameter.id) throw '"id" parameter not informed. Please provide a spreadsheet id.';
var values = SpreadsheetApp.openById(e.parameter.id).getSheets()[0].getRange('A1:J4').getValues();
// I modified below script.
var header = values.shift();
return values.reduce((s, r) => {
r.forEach((c, j, a) => {
s += j == 0 ? `<${header[j]}="${c}">` : `<${header[j]}>${c}<\/${header[j].split(" ")[0]}>`; if (j == a.length - 1) s += `<\/${header[0].split(" ")[0]}>`;
});
return s;
}, "");
}
ผลลัพธ์:
เมื่อเรียกใช้สคริปต์ที่แก้ไขด้านบนจะได้รับผลลัพธ์ต่อไปนี้
<store store-id="F123">
<name>Store One</name>
<address1>123 Street</address1>
<address2 />
<city>London</city>
<postal-code>L67 9JF</postal-code>
<phone>123 456</phone>
<store-hours xml:lang="x-default">10AM | 6PM</store-hours>
<custom-attribute attribute-id="freeTextTitle" xml:lang="x-default">Store Description Title</custom-attribute>
<custom-attribute attribute-id="v3_store_open_hours_0" xml:lang="x-default">11 AM|7 PM</custom-attribute>
</store>
<store store-id="G456">
<name>Store Two</name>
<address1>124 Street</address1>
<address2 />
<city>Manchester</city>
<postal-code>L67 9DS</postal-code>
<phone>124 111</phone>
<store-hours xml:lang="x-default">9AM | 5PM</store-hours>
<custom-attribute attribute-id="freeTextTitle" xml:lang="x-default">Store Description Title</custom-attribute>
<custom-attribute attribute-id="v3_store_open_hours_0" xml:lang="x-default">12 AM|7 PM</custom-attribute>
</store>
<store store-id="J542">
<name>Store Three</name>
<address1>777 High Street</address1>
<address2 />
<city>Leeds</city>
<postal-code>L7 9GG</postal-code>
<phone>555 222</phone>
<store-hours xml:lang="x-default">10AM | 6PM</store-hours>
<custom-attribute attribute-id="freeTextTitle" xml:lang="x-default">Store Description Title</custom-attribute>
<custom-attribute attribute-id="v3_store_open_hours_0" xml:lang="x-default">12 AM|7 PM</custom-attribute>
</store>
บันทึก:
เมื่อคุณใช้ดังกล่าวข้างต้นเป็นผลข้อมูล XML
<contents>{above results}</contents>
ตัวอย่างเช่นผมคิดว่ามันจะต้องแนบเช่น โปรดระวังเรื่องนี้ ดังนั้นหากคุณต้องการส่งออกข้อมูล XML ที่ถูกต้องโปรดใช้สคริปต์ต่อไปนี้ ในกรณีนี้<contents>
คือแท็กตัวอย่างfunction doIt(e) { if (!e) throw 'you cannot run/debug this directly\nyou have to either call the url or mock a call'; if (!e.parameter.id) throw '"id" parameter not informed. Please provide a spreadsheet id.'; var values = SpreadsheetApp.openById(e.parameter.id).getSheets()[0].getRange('A1:J4').getValues(); // I modified below script. var header = values.shift(); var data = values.reduce((s, r) => { r.forEach((c, j, a) => { s += j == 0 ? `<${header[j]}="${c}">` : `<${header[j]}>${c}<\/${header[j].split(" ")[0]}>`; if (j == a.length - 1) s += `<\/${header[0].split(" ")[0]}>`; }); return s; }, ""); return XmlService.getPrettyFormat().format(XmlService.parse(`<contents>${data}$</contents>`)); }
เมื่อคุณแก้ไขสคริปต์ของ Web Apps โปรดปรับใช้ Web Apps เป็นเวอร์ชันใหม่ ด้วยเหตุนี้สคริปต์ล่าสุดจะปรากฏใน Web Apps โปรดระวังเรื่องนี้
โปรดใช้สคริปต์นี้กับการเปิดใช้ V8
อ้างอิง:
- ลด()
- แต่ละ()