Pass dynamic HTML table to spreadsheet

2019-09-21 21:25发布

问题:

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:

In regard to your last question below:

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 Script:

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

This is the Javascript that calls it:

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

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

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

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