0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Google Apps Script で XML スプレッドシート 2003 をダウンロードする(メモ)

Posted at

後で清書予定。

  1. XML を扱うには XmlService が使える。
  2. 任意の処理命令 ( <?処理命令ターゲット 処理内容?> ) を付与する方法は見つからなかったので、 setOmitDeclaration(true) により XML 宣言を消して必要な処理命令を文字列結合で付与。
  3. 各エレメントに対して名前空間を指定し忘れると、 xmlns="" が挿入されるので注意。
  4. ブラウザで開いてダウンロードさせるには downloadAsFile 関数を使う。
  5. XML スプレッドシート 2003 の仕様書はたぶんここ
function doGet() {
  const defaultNamespace = XmlService.getNamespace('urn:schemas-microsoft-com:office:spreadsheet');
  const root = XmlService.createElement('Workbook', defaultNamespace);
  const namespaceSs = XmlService.getNamespace('ss', 'urn:schemas-microsoft-com:office:spreadsheet');
  
  const worksheetElement = XmlService.createElement('Worksheet', defaultNamespace)
      .setAttribute('Name', 'sheet1', namespaceSs);
  root.addContent(worksheetElement);
  
  const tableElement = XmlService.createElement('Table', defaultNamespace);
  worksheetElement.addContent(tableElement);
  
  const rowElement = XmlService.createElement('Row', defaultNamespace);
  tableElement.addContent(rowElement);
  
  const cellElement = XmlService.createElement('Cell', defaultNamespace);
  rowElement.addContent(cellElement);
  
  const dataElement = XmlService.createElement('Data', defaultNamespace)
      .setAttribute('Type', 'String', namespaceSs)
      .setText('test');
  cellElement.addContent(dataElement);
  
  const document = XmlService.createDocument(root);
  const content = XmlService.getCompactFormat().setEncoding('UTF-8').setOmitDeclaration(true).format(document);
  const payload = '<?xml version="1.0"?>' + '<?mso-application progid="Excel.Sheet"?>' + content;
  return ContentService.createTextOutput(payload)
      .setMimeType(ContentService.MimeType.XML)
      .downloadAsFile(Utilities.formatDate(new Date(), 'JTS', "yyyyMMdd'T'hhmmss") + '.xml');
}

参考

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?