Ronnie Headen Ronnie Headen - 2 months ago 22
HTML Question

Google Apps Script - Using Sidebar form to append a row to spreadsheet

This script should append a new row to my spreadsheet 'onclick' of the button, however it doesn't. Ultimately I want to be able to add data to each textbox corresponding to a column and append a new row. Not sure why this doesn't work. How do I append a new row? THis script works in the script editor when I run it, just not in the sidebar html.



// Use this code for Google Docs, Forms, or new Sheets.
function onOpen() {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('SideBar')
.addItem('Open', 'openDialog')

.addToUi();

}

function openDialog() {
var html = HtmlService.createHtmlOutputFromFile("Index");
html.setTitle('Gmf Sent Msg Form');
SpreadsheetApp.getUi().showSidebar(html);

}

function insertRow(){
var doc = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1RY3TZvU- TFhlCyF9TKRk4Hb4b6Q1TnYa5K7ixyktykY/edit#gid=0");
doc.getActiveSheet().appendRow(['700','9/12/16','PO 16-45789'])


}

<body>
Use this form to enter information into the cells of the spreadsheet.<br>
<form>
<p>ID</p>
<input type="text" name="txtID" />
<p>Date</p>
<input type="text" name="txtDate"/>
<p>Subject</p>
<input type="text" name="txtSub" />
<input type="button" value="Append Row" onclick="google.script.run
.withSuccessHandler(insertRow)" />
</form>
</body>




Answer

At this line:

<input type="button" value="Append Row" onclick="google.script.run.withSuccessHandler(insertRow)" />

You're setting the callback function withSuccessHandler() but you're not calling any server-side function, to call it directly you need to use:

google.script.run.insertRow();

If you want to set the callback function to run if the server-side function returns successfully.

google.script.run.withSuccessHandler(onSuccess).insertRow();

For more information, see the guide of communicating with server functions and google.script.run.