Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
885 views
in Technique[技术] by (71.8m points)

google apps script - Pass dynamic HTML table to 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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...