Bir Google E-Tablo Tablosundan XML beslemesi oluşturmak istiyor
Lütfen biraz yardım arıyorum, şimdi çoğu zaman kodla uğraşıyorum ve sıkışıp kaldım ve bu şimdiye kadar bulduğum en iyi çözüm gibi görünüyor.
Bir Google Sayfa tablosundan bir XML dosyası oluşturacak bir komut dosyası oluşturmaya çalışıyorum.
Örnek sayfa şuna benzer >> https://docs.google.com/spreadsheets/d/1tSWMiXBRyhFcAmwpH5tEJiyHCB5vjlGwuHFv865-85A/edit?usp=sharing
Bu kod örneğiyle karşılaştım Google Komut Dosyası Dışa Aktarma E-Tablosu XML Dosyasına ve ihtiyacım olanın% 90'ı ve burada bir web uygulaması olarak yayınlayarak onu çalıştırdım >>https://script.google.com/macros/s/AKfycbxVcUi6dXw0D1CWfZTlwf94gAT9QjqpG__-SaCIHVFVPzftndU/exec?id=1tSWMiXBRyhFcAmwpH5tEJiyHCB5vjlGwuHFv865-85A
Şimdi, XML'in biçimlendirilmesi gerektiğinden, başlıkların ve değerlerin üzerinden geçmesini sağlama konusunda takılı kaldım.
Ayrıca bazı değerlerin özniteliklere sahip olduğuyla karşılaşıyorum, bu nedenle aşağıdaki örnekte 10AM: 18:00 xml: lang = "x-default" ifadesini eklemeyi zor buluyorum
İşte yaratmaya çalıştığım şeyin bir örneği
<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>
Çok teşekkürler
** Daha fazla bağlam eklendi
Teşekkürler, İkisi de, aslında başlıkları ve öznitelikleri eşlemeye çalışırken doIt işlevindeki JavaScript map () işlevine takılı kaldım
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>';
}
değerler, aralıktaki tüm değerleri yakalar, ancak değerleri parçalamaya çalışırken biraz kayboldum.
Harita () işlevinde biraz okuma yaptım, o yüzden başka bir şansım yok
Yanıtlar
Basit bir değişiklik olarak, aşağıdaki değişikliğe ne dersiniz?
Senaryonuzun olarak <sheet>
, <row>
ve <cell>
etiketleri kullanılır. Ancak, bunlar beklenen sonucunuza dahil edilmemiş gibi görünüyor. Her bir etiket olarak 1. satırın başlık satırını kullanmak istediğinizde, bunları komut dosyasında kullanmanız gerekir. Komut dosyanız değiştirildiğinde aşağıdaki gibi olur.
Değiştirilmiş komut dosyası:
Bu değişiklikte, sizin doIt()
değiştirildi.
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;
}, "");
}
Sonuç:
Değiştirilmiş betik üzerinde çalıştırıldığında aşağıdaki sonuç elde edilir.
<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>
Not:
Yukarıdaki sonucu xml verisi olarak kullandığınızda, örneğin, beğeniyi içine almanın gerekli olduğunu düşünüyorum
<contents>{above results}</contents>
. Lütfen buna dikkat edin. Dolayısıyla, geçerli XML verilerini dışa aktarmak istiyorsanız, lütfen aşağıdaki komut dosyasını kullanın. Bu durumda<contents>
örnek bir etikettir.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 Uygulamalarının komut dosyasını değiştirdiğinizde, lütfen Web Uygulamalarını yeni sürüm olarak yeniden dağıtın. Bununla, en son komut dosyası Web Uygulamalarına yansıtılır. Lütfen buna dikkat edin.
Lütfen bu komut dosyasını V8'i etkinleştirirken kullanın.
Referanslar:
- azalt ()
- her biri için()