简体   繁体   English

将数据推送到不同的元素,具体取决于使用 GAS 的谷歌电子表格中的列?

[英]Push data to different elements, depending on column in google spreadsheet with GAS?

I am working in Google Spreadsheet with GAS, and I am trying to push some data from a spreadsheet to an HTML page, and right now that is working.我正在使用 GAS 在 Google 电子表格中工作,我正在尝试将一些数据从电子表格推送到 HTML 页面,现在正在工作。 But I am managing to do is grabbing all the values, and each time it hits a new row, it grabs all the values in that row pushes them into a newly created <div> .但我设法做的是抓取所有值,并且每次遇到新行时,它都会抓取该行中的所有值并将它们推送到新创建的<div> But I would like to do is have some column functionality also so that the different columns gets pushed to a different element like an <input> or a <select> element.但我想做的是也有一些列功能,以便将不同的列推送到不同的元素,如<input><select>元素。

I tried out some things where I declared some variables for the desired columns, and trying to pushing them to the HTML one by one, but it didn't work out.我尝试了一些事情,我为所需的列声明了一些变量,并尝试将它们一一推送到 HTML,但没有成功。

Here is my data:这是我的数据:

在此处输入图片说明

This is what it looks like in the HTML:这是它在 HTML 中的样子:

在此处输入图片说明

This is what I am going for:这就是我要去的地方:

在此处输入图片说明

Would it be better to publish this into tables?将其发布到表格中会更好吗? Because I simply thought of creating divs with classes and set their width and line breaks?因为我只是想用类创建 div 并设置它们的宽度和换行符?

Below is the code I described in the beginning:下面是我在开头描述的代码:

Code.gs 代码.gs
 var ss = SpreadsheetApp.openById("1c7IwmyBrbNq5xwzo-7EyFewCx31WpfP4EzLpkHawffI"); function doGet(request) { return HtmlService.createTemplateFromFile('stuff') .evaluate() .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function include(filename) { return HtmlService.createHtmlOutputFromFile(filename).getContent(); } function getStitchOrders(){ var ordersName = []; var sheet = ss.getSheetByName("Cat1"); var subRange = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()); var orders = subRange.getValues(); for (var i = 0; i < orders.length; i++) { ordersName.push( orders[i] ) } return ordersName; }
stuff.html 东西.html
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>

<div id="orders">
</div>

<script>

$(function() {
google.script.run.withSuccessHandler(buildOrderList).getStitchOrders();
});

function buildOrderList(ordersName) {
var arr = JSON.Parse(ordersName);
var rows = $('#orders');
for (var i = 0; i < arr.length; i++) {
rows.append('<div name="' + arr[i].name + '">' +  arr[i].name  + '</div>');
}
}

</script>

Any suggestions?有什么建议?

Edit编辑

Code2.gs 代码2.gs
 var ss = SpreadsheetApp.openById("1c7IwmyBrbNq5xwzo-7EyFewCx31WpfP4EzLpkHawffI"); function doGet(request) { return HtmlService.createTemplateFromFile('stuff2') .evaluate() .setSandboxMode(HtmlService.SandboxMode.IFRAME); } function include(filename) { return HtmlService.createHtmlOutputFromFile(filename).getContent(); } function getStitchOrders(){ var ordersName = []; var sheet = ss.getSheetByName("Sheet"); var subRange = sheet.getRange(2, 1, sheet.getLastRow(), sheet.getLastColumn()); var orders = subRange.getValues(); for (var i = 0; i < orders.length; i++) { ordersName.push( { name: orders[i][0], status: orders[i][1], comment: orders[i][2] } ); } return JSON.stringify(ordersName); }
stuff2.html 东西2.html
 <script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script> <div id="orders"> </div> <script> $(function() { google.script.run.withSuccessHandler(buildOrderList).getStitchOrders(); }); function buildOrderList(ordersName) { var arr = JSON.Parse(ordersName); var rows = $('#orders'); for (var i = 0; i < arr.length; i++) { rows.append('<div name="' + arr[i].name + '">' + arr[i].name + '</div>'); } } </script>

consider returning the data with this type of pattern:考虑使用这种类型的模式返回数据:

for (var i = 0; i < orders.length; i++) {
  ordersName.push( { 
    name: orders[i][0], 
    status: orders[i][1],
    comment: orders[i][2]
  } );
}
return JSON.stringify(ordersName);

then back in the client-side JS we can turn it back into an Array to loop through:然后回到客户端 JS 中,我们可以将其重新转换为 Array 以进行循环:

function buildOrderList(ordersName) {
  var arr = JSON.parse(ordersName);
  var rows = $('#orders');
  for (var i = 0; i < arr.length; i++) {
    // values can now be referenced via...
    // arr[i].name
    // arr[i].status;
    // arr[i].comment;
    rows.append(...);
  }
}

how to then style the divs and the elements inside them to align up like a table is more a CSS question.然后如何设置 div 和其中的元素的样式以像表格一样对齐更像是一个 CSS 问题。

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

相关问题 如何检查谷歌电子表格中的输入数据是否是 GAS 的有效日期类型 - how to check if the input data in google spreadsheet is a valid Date type by GAS GAS scriplet用于创建包含电子表格数据链接的HTML列 - GAS scriplet to create an HTML column with links from spreadsheet data 通过GAS将数字符号(#)插入Google电子表格 - Insertion of number sign (#) into a Google Spreadsheet via GAS 根据滑块的值将不同的数据推入数组 - Push different data to array depending the value of a slider 如何基于单个单元格编辑(Google Script / GAS)通过电子邮件发送电子表格中的整行数据 - How to send whole row of data in my spreadsheet via email based on single cell edit (Google Script / GAS) GAS:从电子表格数据填充下拉列表 - GAS: Populating a dropdown list from Spreadsheet data Google电子表格过滤器列 - Google Spreadsheet Filter Column 在 Google 电子表格中移动一列 - Moving a column in Google Spreadsheet Google电子表格脚本,该脚本会在工作表中的列的编辑时触发,并根据值覆盖用户定义的单元格的值 - A google spreadsheet script that triggers on edit of a column in a sheet and overwrites the value of a user defined cell or not depending on the value 在GAS中,通过循环读取电子表格中多个工作表中的数据列 - In GAS, reading col of data in multiple sheets in spreadsheet via loop
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM