Mencari untuk membuat umpan XML dari Tabel Google Sheet
Mencari bantuan, tolong, saya telah mengutak-atik kode sekarang hampir sepanjang hari dan saya macet, dan ini tampaknya tentang solusi terbaik yang saya temukan sejauh ini.
Saya mencoba membuat skrip yang akan membuat file XML dari tabel Google Sheet.
Lembar contoh seperti ini >> https://docs.google.com/spreadsheets/d/1tSWMiXBRyhFcAmwpH5tEJiyHCB5vjlGwuHFv865-85A/edit?usp=sharing
Saya menemukan contoh kode ini Google Script Ekspor Spreadsheet ke File XML dan itu 90% yang saya butuhkan dan saya membuatnya berfungsi melalui penerbitan sebagai aplikasi web di sini >>https://script.google.com/macros/s/AKfycbxVcUi6dXw0D1CWfZTlwf94gAT9QjqpG__-SaCIHVFVPzftndU/exec?id=1tSWMiXBRyhFcAmwpH5tEJiyHCB5vjlGwuHFv865-85A
Saya sekarang terjebak untuk membuatnya mengulang header dan nilai karena XML perlu diformat.
Saya juga menemukan beberapa nilai yang memiliki atribut, jadi saya merasa sulit untuk menambahkan xml: lang = "x-default" dalam contoh di bawah ini 10AM: 6PM
Berikut adalah contoh dari apa yang saya coba buat
<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>
Terimakasih banyak
** Menambahkan lebih banyak konteks
Terima kasih, Keduanya, saya sebenarnya terjebak pada fungsi JavaScript map () dalam fungsi doIt mencoba memetakan header & atribut
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>';
}
nilai-nilai mengambil semua nilai dalam rentang tersebut, tetapi saya kehilangan sedikit upaya untuk memecah nilai.
Saya melakukan beberapa pembacaan pada fungsi map () jadi sakit untuk mencoba lagi
Jawaban
Sebagai modifikasi sederhana, bagaimana dengan modifikasi berikut ini?
Dalam skrip Anda <sheet>
,, <row>
dan <cell>
tag digunakan. Tetapi tampaknya ini tidak termasuk dalam hasil yang Anda harapkan. Jika Anda ingin menggunakan baris header dari baris ke-1 sebagai setiap tag, Anda harus menggunakannya dalam skrip. Ketika skrip Anda dimodifikasi menjadi sebagai berikut.
Skrip yang dimodifikasi:
Dalam modifikasi ini, Anda doIt()
telah dimodifikasi.
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;
}, "");
}
Hasil:
Saat skrip yang dimodifikasi di atas dijalankan, hasil berikut diperoleh.
<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>
catatan:
Ketika Anda menggunakan hasil di atas sebagai data xml, misalnya, saya pikir itu harus dilampirkan seperti
<contents>{above results}</contents>
. Harap berhati-hati ini. Jadi jika Anda ingin mengekspor data XML yang valid, gunakan script berikut. Dalam hal ini,<contents>
adalah tag sampel.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>`)); }
Saat Anda memodifikasi skrip Aplikasi Web, terapkan ulang Aplikasi Web sebagai versi baru. Dengan ini, skrip terbaru diterapkan ke Aplikasi Web. Harap berhati-hati ini.
Silakan gunakan skrip ini dengan mengaktifkan V8.
Referensi:
- mengurangi()
- untuk setiap()