简体   繁体   English

如何使用按钮将表格导出为 Google 应用制作工具中的 Google 表格

[英]How to export a table as google sheet in Google app maker using a button

I've looked extensively and tried to modify multiple sample sets of codes found on different posts in Stack Overflow as well as template documents in Google App Maker, but cannot for the life of me get an export and en email function to work.我已经广泛查看并尝试修改在 Stack Overflow 的不同帖子中找到的多个示例代码集以及 Google App Maker 中的模板文档,但我一生都无法获得导出和电子邮件功能。

UserRecords table:用户记录表:

用户记录表

This is the area where the data is collected and reviewed, the populated table :这是收集和审查数据的区域,填充表

填充表

These are the data fields I am working with:这些是我正在使用的数据字段

数据字段

This is what the exported Sheet looks like when I go through the motions and do an export through the Deployment tab:当我完成动作并通过“部署”选项卡进行导出时,导出的工作表如下所示:

导出表

Lastly, this is the email page that I've built based on tutorials and examples I've seen:最后,这是我根据我看到的教程和示例构建的电子邮件页面

电子邮件页面

What I've learned so far (based on the circles I'm going round in):到目前为止我学到的东西(基于我要绕的圈子):

  1. Emails seem mostly straight forward, but I don't need to send a message, just an attachment with a subject, similar to using the code:电子邮件似乎大多是直截了当的,但我不需要发送消息,只需一个带有主题的附件,类似于使用代码:

     function sendEmail_(to, subject, body) { var emailObj = { to: to, subject: subject, htmlBody: body, noReply: true }; MailApp.sendEmail(emailObj); }

Not sure how to change the "body" to the exported document不确定如何将“正文”更改为导出的文档

  1. To straight up export and view the Sheet from a button click, the closest I've found to a solution is in Document Sample but the references in the code speak to components on the page only.要通过单击按钮直接导出和查看工作表,我发现最接近解决方案的是文档示例,但代码中的引用仅与页面上的组件相关。 I'm not sure how to modify this to use the table, and also what to change to get it as a sheet instead of a doc.我不确定如何修改它以使用表格,以及如何更改以将其作为工作表而不是文档。

This may seem trivial to some but I'm a beginner and am struggling to wrap my head around what I'm doing wrong.这对某些人来说似乎微不足道,但我是一个初学者,并且正在努力解决我做错了什么。 I've been looking at this for nearly a week.我一直在看这个近一个星期。 Any help will be greatly appreciated.任何帮助将不胜感激。

In it's simplest form you can do a Google sheet export with the following server script (this is based on a model called employees):在最简单的形式中,您可以使用以下服务器脚本(这是基于称为员工的模型)执行 Google 工作表导出:

function exportEmployeeTable() {

  //if only certain roles or individuals can perform this action include proper validation here

  var query = app.models.Employees.newQuery();
  var results = query.run();

  var fields = app.metadata.models.Employees.fields;

  var data = [];

  var header = [];

  for (var i in fields) {
    header.push(fields[i].displayName);
  }

  data.push(header);

  for (var j in results) {
    var rows = [];
    for (var k in fields) {
      rows.push(results[j][fields[k].name]);
    }
    data.push(rows);
  }

  if (data.length > 1) {
    var ss = SpreadsheetApp.create('Employee Export');
    var sheet = ss.getActiveSheet();
    sheet.getRange(1,1,data.length,header.length).setValues(data);

    //here you could return the URL for your spreadsheet back to your client by setting up a successhandler and failure handler
    return ss.getUrl();
  } else {
    throw new app.ManagedError('No Data to export!');
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM