App Script Add-on Event handler not working - google-apps-script

I am creating an Add-On for Google Spreadsheet. When click the search button nothing seems to happen. How does one go about debugging this ?
I am using the Google Apps Script to create the addon. I am using the example of QuickStart, with some changes to reflect my requirements.
Here is my Code
<div class='sidebar branding-below'>
<form>
<div class="block col-contain">
<div class="block form-group">
<div>
<label for="Query-txt"><b>Query Term</b></label><br/>
<input type="text" name="querytxt" id="Query-txt" value="" class="width-100" />
</div>
<br/>
<div>
<label for="Channel-id-dd"><b>Channel</b></label><br/>
<select id="Channel-id-dd" class="width-100">
<option value="UCpZG4Vl2tqg5cIfGMocI2Ag">CFC India</option>
<option value="UCPPkrC9R5ED2R2JRTaQgETw">NCCF Church</option>
<option value="UCL8wQnv6qB7rZtEYfY3vPtw">River of Life Christian Fellowship</option>
</select>
</div>
<br/>
<div>
<label for="Region-Code-dd"><b>Country Code</b></label><br/>
<select id="Region-Code-dd" class="width-100">
<option value="AU">AU - Australia</option>
<option value="GB">GB - United Kingdom</option>
<option value="US">US - USA</option>
<option value="UAE">UAE - United Arab Emerates</option>
</select>
</div>
<br/>
<div>
<label for="Safety-Type"><b>Safety</b></label><br/>
<select id="Safety-Type" class="width-100">
<option value="moderate">moderate</option>
<option value="strict">strict</option>
<option value="none">none</option>
</select>
</div>
<br/>
<div class="block" id="button-bar">
<button class="blue" id="run-Query">Search</button>
</div>
</div>
</div>
</form>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
$(document).ready(function (){
$('#run-Query').click(function () {
Logger.log("Starting");
var query = $('#Query-txt').val();
var channelid = $('#Channel-id-dd').val();
var countryCode = $('#Region-Code-dd').val();
var safety = $('#Safety-Type').val();
google.script.run
.withSuccessHandler(
alert("success");
)
.withFailureHandler(
alert("failure");
)
.withUserObject(this)
.search_all(query, safety, channelid, countryCode);
Logger.log("Complete");
});
});
</script>

I see two problems with your code. Firstly, Logger.log() is an Apps Script server-side method. It is designed to be called from server-side code, for example in the Code.gs file. What you are writing above is the client-side javascript that is executed on the end user's browser. To log here, you do the same thing you would in regular javascript programming and use the console.log() method instead. This will write logging statements to the javascript console (accessible with Ctrl+Shift+J in Chrome).
Secondly, withSuccessHandler and withFailureHandler take as a parameter the method to call during success and failure scenarios and no parameters. The method you define will get the parameter automatically. Notice how they are used in the Quickstart:
...
.withSuccessHandler(
function(returnSuccess, element) {
element.disabled = false;
})
...
To alert on success, like you have above, you need to use something like this:
...
.withSuccessHandler(
function(returnVal, element) {
alert("Call was successful with return value: " + returnVal);
}
)
...
Using these two methods, you should have the tools necessary to debug your code.

Related

How to set up search option for a charity website

I wonder if you could help me, I'm a graphic designer but trade with super basic HTML experience.
I'm volunteering for a small charity in Birmingham who is looking to create a community planning tool, I said I'll try and help design it, for free of course.
One of main options on the site is people can search to get resources. This is the interface:
Is there a really simple way to link this up?
For example, if someone clicked 'Balsall Heath Ward' and then 'Community' in the drop down menus then clicked search now it will take them to BHcommunity.html which will have resources. Same if somebody clicked 'Moseley Ward' and then clicked 'Sustainability' then search now, it would take them to MWsustainability.html. Does that make sense?
Basically I just need something that commands to point towards links, but I don't know where to start, can anybody give me some advice or help?
If anybody can help I can swop for free design support?
Thanks so much for the support!
Nath
Ps: Here is the code.
<h3 class="demo-panel-title">Fill out the relevant information:</h3>
<div class="col-xs-3"> <div class="form-group has-error"> <input type="text" value="" placeholder="What is your name?" class="form-control" /> </div> </div>
<div class="col-xs-3"> <div class="form-group has-success"> <input type="text" value="" placeholder="What is your email address?" class="form-control" /> <span class="input-icon fui-check-inverted"></span> </div> </div>
<div class="col-xs-3"> <div class="form-group"> <input type="text" value="" placeholder="What organisation are you associated with?" class="form-control" /> </div> </div>
<br/>
<div class="col-xs-3"> <h3 class="demo-panel-title">Which Ward do you want to explore?</h3> <select class="form-control select select-primary" data-toggle="select"> <option value="0">Moseley & Kings Heath Ward</option> <option value="1">Balsall Heath Ward</option> </select> </div>
<br/> <div class="col-xs-3"> <h3 class="demo-panel-title">What data do you need?</h3> <select class="form-control select select-primary" data-toggle="select"> <option value="0">Choose one</option> <option value="1">Local economy</option> <option value="2">Transport</option> <option value="3">Leisure</option> <option value="4" selected>Housing</option> <option value="5">Community</option> <option value="6">Planning</option> <option value="7">Sustainability</option> </select> </div> <br/>
I'd need to add a way to search
I have put together a reasonably simple example of how to do this, linked below. Firstly I have modified your HTML a little, changing the value attributes of your options to be more descriptive. I have added some IDs to elements for ease of location without the need for jQuery as you can see in the small extract here:
<div class="col-xs-3">
<h3 class="demo-panel-title">What data do you need?</h3>
<select id="data-type-select" class="form-control select select-primary" data-toggle="select">
<option value="0">Choose one</option>
<option value="economy">Local economy</option>
<option value="transort">Transport</option>
<option value="leisure">Leisure</option>
<option value="housing" selected>Housing</option>
<option value="community">Community</option>
<option value="planning">Planning</option>
<option value="sustainability">Sustainability</option>
</select>
</div>
In the javascript I have kept it as simple as possible, I hope you can see how I have gathered the information and used it to pick which html file you want to go to.
(function() {
function attachEvents () {
var submitButton = document.getElementById('ward-information-submit-button');
submitButton.addEventListener('click', submitButtonListener);
}
function submitButtonListener () {
getFormData();
}
function getFormData () {
var selectWardElement = document.getElementById('ward-select');
var selectDataTypeElement = document.getElementById('data-type-select');
pickDestination({
selectedWard: selectWardElement.options[selectWardElement.selectedIndex].value,
selectedDataType: selectDataTypeElement.options[selectDataTypeElement.selectedIndex].value
});
}
function pickDestination (options) {
if (options.selectedWard === 'moseley' && options.selectedDataType === 'economy') {
console.log('MWsustainability.html');
window.location = 'MWsustainability.html';
}
if (options.selectedWard === 'balsall' && options.selectedDataType === 'community') {
console.log('BHcommunity.html');
window.location = 'BHcommunity.html';
}
}
attachEvents();
}());
I have a working version of this here which includes some additional comments to hopefully help you make sense of it. You will need to extend this code to make it work for all of your cases and I'm sure there are other requirements you have which you could apply these concepts to. Keep me updated and I'll help you learn where I can.
I appreciate the offer of something in return but it is unnecessary, just keep paying it forward as you are now. Good luck!
https://jsfiddle.net/rvb56sx6/
You may consider making a JSON file or Javascript Object that holds string values for all the links you need. Then make a function that gets the values of the dropdown menus and chooses a link accordingly. Also, consider using Jquery to simplify this process. ( I use it in this example. Meaning that it must be used if you want to use the below function). You can include it by adding the below script tag in your html before you add the script tag for the file with the below function.
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.2/jquery.min.js"></script>
<script src="path/to/JS/File/with/below/function"></script>
Javascript Object
var links = {
balsallHeathWard {
community: '<Link to community>'
...
},
...
}
Javascript Function
$(function goToLink() {
$(document).on('click', '#<submitButtonIdName>', function(){
var ward = $('#<wardChooserSelectTagIdName>').val();
var subchoice = $('#<subChoiceTagIDName>').val();
if(ward == "Balsall Heath Ward") {
if(subchoice == "Community") {
window.location.href = links.balsallHeathWard.community;
} else if ...
...
} else if ...
...
});
})();
The function would look something like the function above. I couldn't complete it because I don't have all the options and links that you have for what can be chosen from the charity website. But this is a general idea. It definitely needs work though, but I am willing to help if you need it (I have a soft spot for charities).

AngularJS: Get value from select and inject HTML based on it

I working on a contact form. The point is it should be accessible without JavaScript. If JavaScript is avaible I want ask visitors about some details based on their purpose of contact.
HTML could look like this (labels are skipped on purpose, to shorten the code):
<form action="">
<select name="purpose">
<option value="hello">Just saying hi</option>
<option value="support">Customer Support</option>
<option value="interview">Interview</option>
</select>
...
<button>Submit</button>
</form>
Well, if I could I would just add some areas that would be shown based on "model". But I can't do it because I don't want to show additional fields to people without JS.
It would be like this:
<form action="" ng-app>
<select name="purpose" ng-model="purpose">
<option value="hello">Just saying hi</option>
<option value="support">Customer Support</option>
<option value="interview">Interview</option>
</select>
...
<div ng-show="purpose == 'support'">
<input type="text" name="customernumber" />
</div>
<div ng-show="purpose == 'interview'">
Sorry, I'm not giving interviews
</div>
...
<button>Submit</button>
</form>
The point is that I will be asking a lot of additional questions. With JS disabled the visitor will be seeing a bloated contact form with all fields and messages.
I'm looking for a solution that would read the value of and inject HTML from JS file into a specific place.
<form action="" ng-app>
<select name="purpose" ng-model="purpose">
<option value="hello">Just saying hi</option>
<option value="support">Customer Support</option>
<option value="interview">Interview</option>
</select>
...
{{injectedHTML}}
...
<button>Submit</button>
</form>
It sounds strange, but everything must be according to the requirements of local municipal office. They want to show these specific fields only to people with JS.
Maybe they think it is good for accessibility. I'm not sure.
Thank you all.
To simply hide set of elements when JavaScript is disabled you could add ng-hide class to elements that should not be displayed. In example:
<form action="" ng-app>
<select name="purpose" ng-model="purpose">
<option value="hello">Just saying hi</option>
<option value="support">Customer Support</option>
<option value="interview">Interview</option>
</select>
...
<div ng-show="purpose == 'support'" class="ng-hide">
<input type="text" name="customernumber" />
</div>
<div ng-show="purpose == 'interview'" class="ng-hide">
Sorry, I'm not giving interviews
</div>
...
<button>Submit</button>
</form>
The ng-hide class sets display: hidden. When JavaScript is enabled ng-show will evaluate the condition and add or remove ng-hide class from element.
Another fairly simple solution would be to use ng-include to load additional content when appropriate - which obviously would only work when JavaScript is enabled - thus making additional fields hidden when JavaScript is not working.
<form action="" ng-app>
<select name="purpose" ng-model="purpose">
<option value="hello">Just saying hi</option>
<option value="support">Customer Support</option>
<option value="interview">Interview</option>
</select>
...
<div ng-if="purpose == 'support'" ng-include="'views/supportField.html'">
</div>
<div ng-if="purpose == 'interview'" ng-include="'views/interviewField.html'">
</div>
...
<button>Submit</button>
</form>
Thanks to miensol I started to think about method using "ng-include". Then I realised one thing: Angular can override the HTML.
So I came with this HTML:
<form ng-controller="ContactController">
<select ng-model="purpose" ng-options="p.title for p in possibilities track by p.value">
<option value="hello">Just saying hi</option>
<option value="support">Customer Support</option>
<option value="interview">Interview</option>
</select>
<div ng-include="purpose.template"></div>
</form>
And this app.js:
(function(){
var app = angular.module('myApp', []);
app.controller('ContactController', ['$scope', function($scope) {
// array with possible reasons, that we use as <option> in <select>
// the "AJ" is added so you can see the difference
$scope.possibilities =
[
{ title: 'AJ Just saying hi', value='hello', template: 'part-support.html'},
{ title: 'AJ Customer Support', value='support', template: 'part-support.html'},
{ title: 'AJ Interview', value='interview', template: 'part-interview.html'}
];
// select default <option>
$scope.purpose = $scope.possibilities[0];
}]);
})();
Still I got feeling it could be written much better (and it's out of my league).

I'm trying to append an option value to a form action

I'm trying to append an option value to a form action, using method get. Submit sends the form request but isn't attaching the select option to the action?
So when the form is submitted it should use >
https://url.here.com?domain= users search here + selected option?
Thanks in advance.
<form action="https://url.here.com?domain=" method="get">
<div class="just-arrived-search">
www.<input type="search" name="domain" placeholder="Search" />
<select>
<option value="">All</option>
<option value=".bike">.bike</option>
<option value=".clothing">.clothing</option>
<option value=".guru">.guru</option>
<option value=".holding">.holdings</option>
<option value=".plumbing">.plumbing</option>
<option value=".singles">.singles</option>
<option value=".venture">.ventures</option>
</select>
<button class="btn-new" type="submit">Register Domain</button>
</div>
</form>
UPDATE: Since you edited your initial post to more clearly explain what you are attempting to achieve, here is a simple solution to your issue.
Use JavaScript to append the selected value to the domain before it submits. Change the button to have an onclick attribute as oppose to making it submit the form.
Add this JavaScript to your head section (or wherever you want, but convention is typically the HEAD section or bottom of the body):
<script type="text/javascript">
function submitForm() {
var ext = document.getElementById('ext');
var selected_opt = ext.options[ext.selectedIndex].value;
// Add own code to handle "All" here
var domain = document.getElementById('domain');
// Append selected option
domain.value = domain.value + selected_opt;
// Submit form
document.getElementById('myForm').submit();
}
</script>
And this is the updated HTML code to go along with it:
<form action="https://url.here.com" method="get" id="myForm">
<div class="just-arrived-search">
www.<input type="search" id="domain" name="domain" placeholder="Search" />
<select id="ext">
<option>All</option>
<option>.bike</option>
<option>.clothing</option>
<option>.guru</option>
<option>.holdings</option>
<option>.plumbing</option>
<option>.singles</option>
<option>.ventures</option>
</select>
<button class="btn-new" onclick="submitForm();">Register Domain</button>
</div>
</form>

Local Storage multiple fields with one field overwriting the second

I am creating a Chrome extension that restricts off-limits TV content. I have two roll-down menu forms that store values to Local Storage.
Javascript (external files):
ratings.js
window.onload=function (){
document.getElementById('saveRatings').onsubmit=saveRatings;
}
function saveRatings() {
var selectedRatings = document.forms["ratings_form"]["ratings"].value;
// store selectedRatings to local storage
localStorage.storedRatings = selectedRatings;
}
age.js
window.onload=function (){
document.getElementById('saveAge').onsubmit=saveAge;
}
function saveAge() {
var selectedAge = document.forms["age_form"]["age"].value;
// store selectedAge to local storage
localStorage.storedAge = selectedAge;
}
HTML
<summary>Select Content to Allow</summary><br>
<form name = "ratings_form" id="saveRatings">
<select name="ratings" multiple="multiple">
<option value="G">G only</option>
<option value="G/PG">G/PG only</option>
<option value="G/PG/PG13">G/PG/PG-13 only</option>
<option value="G/PG/PG13/R">G/PG/PG-13/R</option>
</select>
<div></div>
<input type="submit" value="Save"> </form>
<summary>Select Age Group to Deter</summary><br>
<form name = "age_form" id="saveAge">
<select name="age" multiple="multiple">
<option value="e">Everyone</option>
<option value="ct">Children & Teens;</option>
<option value="c">Children</option>
<option value="0">Turn off</option>
</select>
<div></div>
<input type="submit" value="Save">
</form>
The key-value pair for age_form stores correctly. However, ratings_form always gives me undefined. If I switch up the order (age first and ratings next), then the key-value pair for ratings_form would give me the correct value whereas age_value would give me undefined. It seems like the second form values are overwriting the first form values. How can I prevent this overwriting from occurring.
Thanks for your help.
Of course, your problem is that you're overwriting the window.onload function with whichever code runs last! All you need is a simple console.log(); in each function to see that the first one is not being called. You can remedy this with addEventListener() or by using jQuery's $(document).ready().
window.addEventListener('load', function() {
console.log('1');
});
window.addEventListener('load', function() {
console.log('2');
});
Just remember onload is a property of window and acts just like any variable/property would. Consider this:
var foo = 'bar';
foo = 'baz';
console.log(foo); // displays 'baz', of course! You changed the value!
That is just what you did with the onload function. You changed it to something else.

submitting a text search using jsoup

I have a piece of html code which represents a part of a website that is supposed to be the search widget for a directory of a faculty in a university
<div id="right_column" class="content_main">
<div class="searchbox">
<form method="POST" action="/faculty/directory_search/" id="searchform">
<h4>Search the Directory</h4>
<input type="text" name="searchterms" value="" />
<select name="category" class="dropdown"> <option value="all" selected="selected">All Categories</option> <option value="Faculty">Faculty</option> <option value="Staff">Staff</option> <option value="Visitors">Visitors</option> <option value="Full time">Full time</option> <option value="Visiting">Visiting</option> <option value="Special Appointment">Special Appointment</option> <option value="Biological Sciences">Biological Sciences</option> </select>
<input type="hidden" name="sort" value="asc" />
<input type="submit" class="submit" value="Search directory" />
<a class="button" href="/faculty/index/desc" id="sortbutton">Sort Alphabetically</a>
</form>
<script type="text/javascript">
$('#searchform').ready(function(){
$('#sortbutton').click(function(){
$('input[name="sort"]').val('desc');
$('#searchform').submit();
return false;
});
});
</script>
</div>
I am trying to input the name "john" and submit the search using jsoup using the following java code (intended for android but it's overall the same java code as for a regular java app)
Document doc = Jsoup.connect("http://www.qatar.cmu.edu/directory/").data("searchterms", "john").post();
However, I keep getting the same page as just "http://www.qatar.cmu.edu/directory/" with no search submitted. I noticed that in the html code there is the submit input type. I'm wondering if I had to submit the search. If so, how can it be done?
I believe you are performing a POST request to the page containing the form, not the forms endpoint. This should work:
Document doc = Jsoup.connect("http://www.qatar.cmu.edu/faculty/directory_search/").data("searchterms", "john").data("sort", "asc").data("category", "all").post();
It makes the POST request directly to the forms endpoint.