Google Sheet Table에서 XML 피드를 생성하려고합니다.
제발 도움을 구하고, 나는 지금 대부분의 날 코드를 다루고 있었고, 지금까지 찾은 최고의 솔루션에 관한 것 같습니다.
Google Sheet 테이블에서 XML 파일을 만드는 스크립트를 만들려고합니다.
예제 시트는 다음과 같습니다. >> 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의 형식을 지정해야하므로 헤더와 값을 반복하는 데 고착되어 있습니다.
또한 일부 값에 속성이 있으므로 아래 예제 10AM : 6PM에 xml : lang = "x-default"를 추가하는 것이 까다 롭다는 것을 알았습니다.
다음은 내가 만들려고하는 것의 예입니다.
<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>
많은 감사
** 더 많은 컨텍스트 추가
고마워요, 둘 다 헤더와 속성을 매핑하려는 doIt 함수의 JavaScript map () 함수에 실제로 붙어 있습니다.
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>';
}
값은 범위의 모든 값을 가져 오지만 값을 나누는 데 약간의 손실이 있습니다.
나는 map () 함수에서 약간의 읽기를 했으므로 다른 일이 있습니다.
답변
간단한 수정으로 다음 수정은 어떻습니까?
스크립트에서 <sheet>
, <row>
및 <cell>
태그가 사용됩니다. 그러나 이들은 예상 결과에 포함되지 않은 것 같습니다. 첫 번째 행의 헤더 행을 각 태그로 사용하려면 스크립트에서 사용해야합니다. 스크립트를 수정하면 다음과 같이됩니다.
수정 된 스크립트 :
이 수정에서 귀하 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을 활성화 한 상태에서이 스크립트를 사용하십시오.
참고 문헌 :
- 줄이다()
- 각각()