I am trying to append the HTML table (user input) to Google Sheets. I read this post but the solution did not work for me. I've enabled Google Sheets API in Apps Script. I can use appendrow to update each single row per click, but I look forward to appending the whole table with a single click.
HTML front end.
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<table id="t01">
<tr>
<th>Project</th>
<th>Current Light Type</th>
<th>Qty</th>
</tr>
<tr>
<td><input type="text" id="p1" size="25"></td>
<td>
<select id="l1">
<option>2D</option>
<option>Donwlight</option>
</select>
</td>
<td><input type="text" id="q1" size="25"></td>
</tr>
<tr>
<td><input type="text" id="p2" size="25"></td>
<td>
<select id="l2">
<option>2D</option>
<option>Donwlight</option>
</select>
</td>
<td><input type="text" id="q2" size="25"></td>
</tr>
</table>
<div>
<button id="btn">Submit</button>
</div>
<script>
document.getElementById("btn").addEventListener("click",parse_to_backend);
function parse_to_backend(){
let table = document.getElementById("t01").outerHTML;
google.script.run.pasteHtml(table);
document.getElementById("p1").value="";
document.getElementById("l1").value="";
document.getElementById("q1").value="";
};
</script>
</body>
</html>
JS backend
function doGet(e) {
return HtmlService.createTemplateFromFile("page").evaluate();
}
function pasteHtml(table) {
var url = "https://docs.google.com/spreadsheets/d...";
var ss = SpreadsheetApp.openByUrl(url);
var ws = ss.getSheetByName("Data");
var req = {
requests: [
{
pasteData: {
html: true,
data: table,
coordinate: {
sheetId: ws,
rowIndex: 0,
columnIndex: 0,
},
},
},
],
};
Sheets.Spreadsheets.batchUpdate(req, ws);
}