简体   繁体   中英

Using JavaScript in Google Scripts to transfer information to spreadsheet, but spreadsheet shows undefined

I am using Google Scripts UiApp in order to gather availability information. I want to send this information to a spreadsheet. I have used the example here: http://www.googleappsscript.org/advanced-examples/insert-data-in-sheet-using-ui-forms to get me started in the right direction.

The Web App looks good and when clicking submit, the appropriate message displays. However, the values that are transferred to the spreadsheet say "undefined" for all of the entries.

How can I convince it to link the textbox entered data to the variables so that I can transfer to the spreadsheet?

Thanks!!

Here is some code:

var submissioSSKey = // Key removed

function doGet() {
var rows = 15
var columns = 15
var mygrid = UiApp.createApplication().setTitle("MLC Walk Ins Scheduling")
var panel = mygrid.createSimplePanel();

// Define the grid layout
var grid = mygrid.createGrid(rows, columns).setCellPadding(2).setCellSpacing(8)

// Create the text at the top
var Title = mygrid.createLabel("Walk-In Scheduling")
grid.setWidget(1, 1, Title)

(snip) - creating various checkboxes and textboxes

  var text1 = mygrid.createTextBox().setName('name1')
  grid.setWidget(3,9,text1)
  var text6 = mygrid.createTextBox().setName('message1')
  grid.setWidget(4,9,text6)
// Create the "submit" button
 var submit_button = mygrid.createButton("Submit")
grid.setWidget(12,9,submit_button)

 var infoLabel = mygrid.createLabel('Availability inserted    successfully.').setVisible(false).setId('info');
 grid.setWidget(13,9,infoLabel)

var handler = mygrid.createServerClickHandler('insertInSS');
handler.addCallbackElement(panel);
submit_button.addClickHandler(handler);  

panel.add(grid);
mygrid.add(panel);
mygrid.add(grid);
return mygrid
}

Then the function call for the button:

//Function to insert data in the sheet on clicking the submit button
function insertInSS(e){
var mygrid = UiApp.getActiveApplication()
var name1 = e.parameter.name1
var message1 = e.parameter.message1
 mygrid.getElementById('info').setVisible(true).setStyleAttribute('color','blue')

var sheet = SpreadsheetApp.openById(submissioSSKey).getActiveSheet()
var lastRow = sheet.getLastRow()
var targetRange = sheet.getRange(lastRow+1, 1, 1, 2).setValues([[name1,message1]])

return mygrid
}

Ahh! A simple fix for a big headache.

I had an extra line:

mygrid.add(grid);

that was breaking it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM