Google脚本将电子表格导出到XML文件 [英] Google Script Export Spreadsheet to XML File

查看:64
本文介绍了Google脚本将电子表格导出到XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在运行脚本的Google电子表格中创建一个菜单按钮.此脚本应提取特定的行和列,然后将它们导出到XML文件.

I am trying to create a menu button in my Google Spreadsheet that runs a script. This script should extract specific rows and columns, then export them to an XML file.

我正在尝试使用ContentService编写xml文件,但是它似乎不起作用.有没有可以帮助您的示例或教程?

I am trying to write the xml file using the ContentService, but it does not seem to work. Is there an example or tutorial that can help?

谢谢

推荐答案

您是否收到任何错误消息?或者,什么是行不通的?

Are you getting any error messages? Or, what specifically is not working?

我刚刚测试了它,看起来不错,这是我的代码段

I've just tested it and it seems fine, here's my snippet

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:B2').getValues();
  return '<sheet>' + values.map(function(row, i) {
    return '<row>' + row.map(function(v) {
      return '<cell>' + v + '</cell>';
    }).join('') + '</row>';
  }).join('') + '</sheet>';
}

顺便说一句,我已经将此脚本作为用户运行的网络应用程序发布,供任何人测试: https://script.google.com/macros/s/AKfycbwrctRyspI2LnZ5hP8CMm7yY96jebLQS_4LShaKr_RIUKLm9qg/exec?id= paste-a-spreadsheet-id-here

By the way, I've published this script as a web-app running as the user for anyone to test: https://script.google.com/macros/s/AKfycbwrctRyspI2LnZ5hP8CMm7yY96jebLQS_4LShaKr_RIUKLm9qg/exec?id=paste-a-spreadsheet-id-here

这篇关于Google脚本将电子表格导出到XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆