Pass dynamic HTML table to spreadsheet

时间:2019-01-18 18:14:20

标签: html google-apps-script google-sheets

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:

Pass HTML Table to Google Spreadsheet. Get Cell Values out of a Dynamic Table

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.

Thanks in advance.

1 个答案:

答案 0 :(得分:0)

关于下面的最后一个问题:

这是一种方法。我倾向于在服务器端执行这种操作,然后将其作为对象传递给html文件。这是我以前的项目中的一些代码。它并不是要完全满足您的需求,但您可以将其用作起点。

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:

$(function() {//requires appropriate JQuery resource or you could us windows.onload

      google.script.run
         .withSuccessHandler(updateSelect)
         .getAllSpreadSheets();
    });

这是更新电子表格的Javascript:

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脚本:

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;
}