Google Sheets Script Sidebar button not activating function - javascript

I've spent hours trying to figure this out, since I feel like I should do that before asking a question here. Searched, looked at answers, tried stuff. I think I learn that way and expand my skills/knowledge.
At any rate, I'm trying to just get started using a sidebar to make my sheets script app work better, and testing in small pieces. I want the Clear Old button (at this point) to simply open an alert box with the date in it, fed from a text box generated by datePicker.
Here is the HTML:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>jQuery UI Datepicker - Default functionality</title>
<link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
<link rel="stylesheet" href="https://ssl.gstatic.com/docs/script/css/add-ons1.css">
<script src="https://code.jquery.com/jquery-1.12.4.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<script>
$( function() {
$( "#datepicker" ).datepicker();
} );
</script>
<base target="_top">
</head>
<body>
<p><strong>Clear Old Entries</strong></p>
<p>Clear Entries before</p>
<p><input type="text" id="datepicker"></p>
<p><input type="submit" value="Clear Old" onClick="google.script.run.ClearOld(document.getElementById(datepicker).value)" /></p>
<p><input type="button" value="Close Sidebar" onClick="google.script.host.close()" /></p>
</body>
</html>
And here is the script code that gets the menu item and the sidebar, as well as the function I'm trying to use as a short term test:
function onInstall(e) {
onOpen(e);
}
function onOpen(e) {
SpreadsheetApp.getUi() // Or DocumentApp or FormApp.
.createMenu('Bulletinator')
.addItem('Create Bulletin', 'bulletinatorSidebar')
.addItem('Clear Old', 'clearOldSideBar')
.addToUi();
}
function bulletinatorSidebar() {
var html = HtmlService.createHtmlOutputFromFile('BullV2Side')
.setTitle('Bulletinator')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
function clearOldSideBar() {
var html = HtmlService.createHtmlOutputFromFile('ClearOldSide')
.setTitle('Clear Old Entries')
.setWidth(300);
SpreadsheetApp.getUi().showSidebar(html);
}
var today;
var todayDay;
var todayDate;
var ui = SpreadsheetApp.getUi();
var sh = SpreadsheetApp.getActiveSheet();
var rg = sh.getDataRange();
var vA = rg.getValues();
var rowsFound = 0;
var rowsDeleted = 0;
function ClearOld(value) {
var clearDate=value;
var testThis = ui.alert(
'Clear entries prior to',
clearDate,
ui.ButtonSet.YES_NO);
When I click the button in the sidebar there is no response, and nothing shows up in the execution transcript.
On a side note, I also struggled with being able to have two datePickers in the sidebar; only one worked. So that is why this setup has two separate sidebars getting called.
Thanks for any help.

It seems, the datepicker variable is not defined. you have to use the next code
onClick="google.script.run.ClearOld(document.getElementById('datepicker').value)"
or
onClick="google.script.run.ClearOld($('#datepicker').val())"

Related

How can I directly display a value from an excel cell without ActiveX? [duplicate]

I'm trying to create a trigger button that, when pressed, will display a value from an excel cell in alert box or on the page.
below is all I could get, but it doesn't display anything on the page.
update: I managed to do this using ActiveX, but I don't want to use this library for some reasons. Do you have any idea how I could do it?
update code:
<html>
<head>
<script src="https://cdnjs.cloudflare.com/ajax/libs/xlsx/0.15.6/xlsx.full.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<title>Excel to HTML</title>
</head>
<body>
<p>Press to show</p>
<input type="button" onclick="ReadData(4, 2) ; msgprint()" value="Show" />
<div id="div1">
<script>
function ReadData(cell, row) {
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("mypah/myworkbook.xlsx");
var excel_sheet = excel.Worksheets("Sheet1");
var data = excel_sheet.Cells(cell, row).Value;
document.getElementById("div1").innerText = data;
}
</script>
</div>
<script>
function msgprint() {
alert(document.getElementById("div1").innerText);
}
</script>
</body>
</html>

How can I save any changes on my github website when made changes from diffrent devices

<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=2.0, user-scalable=yes" />
<title>Who is working on the table</title>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet" />
<link href="css/custom.css" rel="stylesheet" />
<style type="text/css">
</style>
</head>
<body>
<ul class="list-group">
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox" value="yes"> Alex is working on the table.</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox3" value="yes"> Tom is working on the table.</li>
<li class="list-group-item"><input type="checkbox" class="save-cb-state" name="mycheckbox2" value="yes"> Lisa is working on the table.</li>
</ul>
</div>
<script src="http://code.jquery.com/jquery.js"></script>
<!-- This JavaScript file is required to load the XpressDox interview as well as the code required to run it -->
<script>
// Avoid scoping issues by encapsulating code inside anonymous function
(function() {
// variable to store our current state
var cbstate;
// bind to the onload event
window.addEventListener('load', function() {
// Get the current state from localstorage
// State is stored as a JSON string
cbstate = JSON.parse(localStorage['CBState'] || '{}');
// Loop through state array and restore checked
// state for matching elements
for(var i in cbstate) {
var el = document.querySelector('input[name="' + i + '"]');
if (el) el.checked = true;
}
// Get all checkboxes that you want to monitor state for
var cb = document.getElementsByClassName('save-cb-state');
// Loop through results and ...
for(var i = 0; i < cb.length; i++) {
//bind click event handler
cb[i].addEventListener('click', function(evt) {
// If checkboxe is checked then save to state
if (this.checked) {
cbstate[this.name] = true;
}
// Else remove from state
else if (cbstate[this.name]) {
delete cbstate[this.name];
}
// Persist state
localStorage.CBState = JSON.stringify(cbstate);
});
}
});
})();
</script>
</body>
</html>
The code shows 3 checkboxes. I want to save changes made from any location and devices. For example Alex checked the first box, I want to edit the table, but I that Alex is working on it. After he finished Alex unchecks the box and I refresh the page, so that i can see Alex is done with his work...

How to add event with Javascript to an html tag

I've a landingpage with dynamic html tags.
The Problem is, that i can't select directly the tag. Its a link.
the following code is the construct:
<div id="testid"><div><div>Button 1<div><div><div>
Every time someone clicks on the link (a-tag) I want to fire an event like the following code:
Button 1
the question: what is the Javascript code to add the onclick="dataLayer.push({'event': 'button1-click'}) attribute to the a tag.
I tried the following code:
var d = document.getElementById("testid").firstchild;
d.setAttribute("onclick", "dataLayer.push({'event': 'button1-click'})");
but it seems to the code is incorrect. The a tag is also not the first child; there are 2 divs between :(
Use querySelector and addEventListener:
document.addEventListener('DOMContentLoaded', function () {
var dataLayer = [];
var d = document.querySelector("#testid a[name=button1]");
d.addEventListener("click", function () {
dataLayer.push({ 'event': 'button1-click' });
});
});
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>repl.it</title>
<link href="style.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="testid">
<div>
<div>
Button 1
<div>
<div>
<div>
</div>
</div>
</div>
</div>
</div>
</div>
<script src="script.js">
</script>
</body>
</html>
There's a few things you were missing from your JS.
Using a more specific selector (#testid a[name=button1] vs. the firstchild of #testid, which was not accurate).
Wrapping all the code in a DOMContentLoaded listener. JS that depends on elements on a page needs to wait for the page to build first, that's what DOMContentLoaded is.
Check out my solution. Hope this helps.
var d = document.querySelector("#testid a");
var dataLayer = []
d.onclick = function () {
dataLayer.push({'event': 'button1-click'})
console.log(dataLayer.length)
}
<div id="testid"><div><div>Button 1<div><div><div>

How do I change the button to say "Copied!" after text is copied to clipboard using clipboard.js?

The following HTML page is from a demo for clipboard.js from clipboardjs.com. I like how this works, where it highlights the text.
However, I have a requirement to have it change the button from displaying "Copy" to "Copied!" once the user has clicked on the button and it has successfully completed the copy to clipboard.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>target-input</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<!-- 1. Define some markup -->
<input id="foo" type="text" value="hello">
<button class="btn" data-clipboard-action="copy" data-clipboard-target="#foo">Copy</button>
<!-- 2. Include library -->
<script src="../dist/clipboard.min.js"></script>
<!-- 3. Instantiate clipboard -->
<script>
var clipboard = new Clipboard('.btn');
clipboard.on('success', function(e) {
console.log(e);
});
clipboard.on('error', function(e) {
console.log(e);
});
</script>
</body>
</html>
This worked for me:
clipboard.on('success', function(e) {
e.clearSelection();
e.trigger.textContent = 'Copied!';
});
Quoting from this tutorial on the part of Working with the Custom Events:
First, we clear selection within the area of the copied content using the clearSelection() utility function from Clipboard. Adding this is optional.
Then we set the content to “Copied!”
clipboard.on('success', function(e) {
document.querySelector('data-clipboard-target="#foo"').value = 'Copied!';
});

Handlebarsjs : H1 tag won't display when submit button is clicked

I'm messing around with Handlebars.js and I'm doing a very simple example consisting of a tutorial from a website(the shoes section) and my own simple little template(the heading, where the problem is)
handlebardemo.html
<head>
<meta charset="ISO-8859-1" name="viewport" content="width=device-width, initial-scale=1">
<title>Handlebar demo</title>
<script src="lib/jquery-2.1.4.js" type="text/javascript"></script>
<script src="lib/jquery-ui.js" type="text/javascript"></script>
<script src="lib/handlebars-v4.0.4.js" type="text/javascript"></script>
<script src="js/main.js" type="text/javascript"></script>
<link rel="stylesheet" href="css/w3.css">
<link rel="stylesheet" href="css/jquery-ui.css">
</head>
<body>
<div class="username-container">
<script id="heading-template" type="x-handlebars-template">
<h1 class="h1">Hello {{username}}</h1>
</script>
<input type="text" id="username">
<button type="submit" id="username-submit">Enter</button>
</div>
<h4 class="w3-row-padding">Shoe List:</h4>
<ul class="shoesNav w3-ul w3-text-indigo"></ul>
<script id="shoe-template" type="x-handlebars-template">
{{#each this}}
<li class="shoes">{{name}}--Price:{{price}} </li>
{{/each}}
</script>
</body>
main.js
$("#username-submit").click(function(){
var uNameVal = $("#username").val();
var uName = {"username":uNameVal};
var theTemplateScript = $("#heading-template").html();
var theTemplate = Handlebars.compile(theTemplateScript);
$(".username-container").append(theTemplate(uName));
});
$(function (){
var shoesData=[{name:"Nike", price:199.00}, {name:"Loafers", price:59.00}, {name:"Wing Tip", price:259.00}];
//Get html from tempalte in script tag
var theTemplateScript = $("#shoe-template").html();
//Compile the template
var theTemplate = Handlebars.compile(theTemplateScript);
$(".shoesNav").append(theTemplate(shoesData));
//shoesData is passed to compiled handlebars function
//function inserts values from the objects in their respective places in the HTML
//and returned HTML: as a string. Then jQuery is used to append the resulting HTML string to the page
})
Im not sure if I'm using the handlbars syntax etc. correctly, but i based it mostly off of the second function in main.js, which came from a brief handlebars tutorial
When I click the Enter button, nothing happens. There are no console errors, it just does nothing. Am I going about this the wrong way, at least compared to the unordered list example?
EDIT: As per Elli Parks answer, I added an id to the submit button and changed the click handler assignment to the submit button rather than the textbox(a silly mistake on my part). The element still won't appear when the submit button is clicked
In main.js, you're attaching the click handler to #username, which is an input field. You need to give the Enter button an id (ex: #username-submit) and attach the click handler to the button.
So something like this:
handlebardemo.html
<button type="submit" value="Enter" id="username-submit" >Enter</button>
main.js
$("#username-submit").click(function(){
var uNameVal = $("#username").val();
var uName = {"username":uNameVal};
var theTemplateScript = $("#heading-template").html();
var theTemplate = Handlebars.compile(theTemplateScript);
$(".username-container").append(theTemplate(uName));
});
You need to change two things:
fix the selector to target the button, like #ElliPark has already said
put the whole thing into the document-ready handler (ie, into the $(function () {...} ); construct. You are trying to attach the event listener before the DOM is ready.
See the demo on JSBin.

Categories

Resources