将动态HTML表传递到电子表格 [英] Pass dynamic HTML table to spreadsheet
问题描述
我有一个动态HTML表,我想通过GS代码将其值传递给Google Spreadsheet.
I have a dynamic HTML table which I want to pass its values to a Google Spreadsheet through GS code.
<div class="container">
<table style="width: auto" id="myTable" class=" table order-list">
<!-- ### INICIO DO CODIGO DA TABELA TD = TABLE DATA / TR = TABLE ROW / TH = TABLE HEADING### -->
<!-- CABEÇALHO -->
<tr>
<th>Ref.</th>
<th>Produto</th>
<th>Categoria</th>
<th>Marca</th>
<th>Tipo de Venda</th>
<th>$ Etiqueta</th>
<th>$ Real</th>
<th>Quantidade</th>
</tr>
<!-- LINHAS -->
<tr>
<td><input type="text" name="refProd" size="10" /></td>
<td><input type="text" name="nomeProd" size="30" /></td>
<td><input type="text" list="categorias" name="catProd" size="15" /></td>
<td><input type="text" list="marcas" name="fabProd" size="10" /></td>
<td><input type="text" list="carater" name="tipoVenda" size="15" /></td>
<td><input type="number" name="vendaEtiqueta" /></td>
<td><input type="number" name="vendaReal"/></td>
<td><input type="number" name="qtd" /></td>
</tr>
</table>
</div>
我已经遵循了这个答案:
I've already followed this answer:
通过HTML表格到Google电子表格.从动态表中获取单元格值
到达在HTML文件中生成数组的部分,但是我不知道如何以将这些值添加到电子表格的方式将HTML值传递给GS代码.有谁可以帮助我吗?我是这个编程世界的初学者.
And reached the part where I generate the array in the HTML file, but I don't know how to pass the HTML values to the GS code in a way where I can add those values to the spreadsheet. Can someone help me please? I'm a beginner in this programming world.
谢谢.
推荐答案
关于下面的最后一个问题:
In regard to your last question below:
这是一种方法.我倾向于在服务器端执行这种操作,然后将其作为对象传递给html文件.这是我以前的项目中的一些代码.它并不是要完全满足您的需求,但您可以将其用作起点.
This is one way to go. I tend to do this sort of thing server side and then pass it as an object to the html file. This is some code from a previous project of mine. It's not intended to be an exact match for your needs but you can use it as a starting point.
Google脚本:
function htmlSpreadsheet(ssO)
{
var br='<br />';
var s='';
var hdrRows=1;
var ss=SpreadsheetApp.openById(ssO.id);
var sht=ss.getSheetByName(ssO.name);
var rng=sht.getDataRange();
var rngA=rng.getValues();
s+='<table>';
for(var i=0;i<rngA.length;i++)
{
s+='<tr>';
for(var j=0;j<rngA[i].length;j++)
{
if(i<hdrRows)
{
s+='<th id="cell' + i + j + '">' + '<input id="txt' + i + j + '" type="text" value="' + rngA[i][j] + '" size="20" onChange="updateSS(' + i + ',' + j + ');" />' + '</th>';
}
else
{
s+='<td id="cell' + i + j + '">' + '<input id="txt' + i + j + '" type="text" value="' + rngA[i][j] + '" size="20" onChange="updateSS(' + i + ',' + j + ');" />' + '</th>';
}
}
s+='</tr>';
}
s+='</table>';
s+='</body></html>';
var namehl=Utilities.formatString('<h1>%s</h1>', ss.getName());
var shnamehl=Utilities.formatString('<h2>%s</h2>', sht.getName());
var opO={hl:s,name:namehl,shname:shnamehl};
return opO;
}
这是调用它的Javascript:
This is the Javascript that calls it:
$(function() {//requires appropriate JQuery resource or you could us windows.onload
google.script.run
.withSuccessHandler(updateSelect)
.getAllSpreadSheets();
});
这是更新电子表格的Javascript:
This is the Javascript that updates the spreadsheet:
function updateSS(i,j)
{
var str='#txt' + String(i) + String(j);
var value=$(str).val();
var ssId=$('#sel1').val();
var name=$('#sel2').val();
var updObj={rowIndex:i,colIndex:j,value:value,id:ssId,name:name};
$(str).css('background-color','#ffff00');
google.script.run
.withSuccessHandler(updateSheet)
.updateSpreadsheet(updObj);
}
这是用于更新电子表格的Google脚本:
and this is the Google Script that updates the spreadsheet:
function updateSpreadsheet(updObj)
{
var i=updObj.rowIndex;
var j=updObj.colIndex;
var value=updObj.value;
var ss=SpreadsheetApp.openById(updObj.id);
var sht=ss.getSheetByName(updObj.name);
var rng=sht.getDataRange();
var rngA=rng.getValues();
rngA[i][j]=value;
rng.setValues(rngA);
var data = {'message':'Cell[' + Number(i + 1) + '][' + Number(j + 1) + '] Has been updated', 'ridx': i, 'cidx': j};
return data;
}
这篇关于将动态HTML表传递到电子表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!