Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

error submit form google apps script bad value

I have an application that submits a form. I'm sure that my code is fine, but when I execute the application I run into this error: error encountered script bad value.

Here are my two functions: doGet writes the form and onSumbit submits the form into the spreadsheet

function doGet()
{
var Myapp= UiApp.createApplication();
var panel=Myapp.createAbsolutePanel().setStyleAttributes({'position':'fixed',
                                                        'top':'15%',
                                                        'left':'2%',
                                                        'height':'300',
                                                        'width':'500',});
var NameLabel=Myapp.createLabel().setText('Name');
var NameTb=Myapp.createTextBox().setId('Nameid').setName('Name');
var LastNameLabel=Myapp.createLabel().setText('Last name');
var LastNameTb=Myapp.createTextBox().setId('Lastnameid').setName('LastName');
var emailLABEL=Myapp.createLabel().setText('Email');
var emailtb=Myapp.createTextBox().setName('Email');
var serverHandler=Myapp.createServerHandler('onSubmitForm');
var ButtonSubmit=Myapp.createButton('submit',serverHandler);
   serverHandler.addCallbackElement(NameTb).addCallbackElement(LastNameTb).addCallbackElement(emailtb);
var panelButton =Myapp.createFlowPanel().setStyleAttributes({'position': 'fixed', 
                                                           'border' : '2px solid black',
                                                           'top' : '80%',
                                                           'left' : '70%',
                                                           'width' : '55', 
                                                           'height':'30'}); 

Myapp.add(panel);
Myapp.add(panelButton);
panel.add(NameLabel);
panel.add(NameTb);
panel.add(LastNameLabel);
panel.add(LastNameTb);
panel.add(emailLABEL);
panel.add(emailtb);
panelButton.add(ButtonSubmit);

return Myapp;
}
function onSubmitForm(e)
{
var Myapp = UiApp.getActiveApplication();

var sheet     =SpreadsheetApp.openById('0Ar2BlsmmO4nZdHN6WXp2akx1QWUzX3lKX19rYzUyRVE&usp').getActiveSheet();

var numRows = sheet.getLastRow(); 

sheet.getRange("A"+lastRow).setValue(e.parameter.Name);
sheet.getRange("B"+lastRow).setValue(e.parameter.LastName);
sheet.getRange("C"+lastRow).setValue(e.parameter.Email);

return Myapp;
}

Thank you for your help.

like image 321
Marooweb Avatar asked Apr 06 '26 22:04

Marooweb


1 Answers

The error seems to come from the spreadSheetApp call, does this Id exist and have you edit access to it ?

Another point : don't use getActiveSheet() in that context as there is no spreadsheet open so no active sheet .... use getSheetByName() instead.

Last point : you are using a variable lastRow that does not exist.... you defined numRows , well use it ! (but add +1 otherwhise you will overwrite the last row each time you submit.

Here is the replacement code :(I used another ID for my test, revise the value and dubble check it)

var sheet = SpreadsheetApp.openById('0AnqSFd3iikE3dGFpMm1IMHo5OGZteUZSTEV6NnRUT2c').getSheetByName('Sheet1');

var numRows = sheet.getLastRow()+1; 

sheet.getRange("A"+numRows).setValue(e.parameter.Name);
sheet.getRange("B"+numRows).setValue(e.parameter.LastName);
sheet.getRange("C"+numRows).setValue(e.parameter.Email);

And finally, why didn't you use a more usual doGet / doPost structure, it would simplify the script as it doesn't need callbackElement and handler... ? Also, think about adding something in the handler function to tell the user that his form has been submitted otherwise they will click it indefinitely ;-)

like image 178
Serge insas Avatar answered Apr 11 '26 04:04

Serge insas



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!