I'm creating an apps script add-on for google docs(word-like), I created a sidebar with check boxes and a button and want to print specific text into the document after I click the button and depending on which check boxes I have highlighted, so far I managed to create the sidebar with the Html components but I can't manage to print the text in the document.
<link href="https://ssl.gstatic.com/docs/script/css/add-ons.css" rel="stylesheet">
<div class="sidebar">
<div class="elements">
<input type="checkbox" id="myCheck1">Check Box 1<br>
<input type="checkbox" id="myCheck2">Check Box 2<br><br>
<button class="blue" id="process" onclick="myFunction()">Print</button><br><br>
<p id="text1" style="display:none">Checkbox1 is CHECKED!</p>
<p id="text2" style="display:none">Checkbox2 is CHECKED!</p>
</div>
</div>
<script>
function myFunction() {
var checkBox1 = document.getElementById("myCheck1");
var checkBox2 = document.getElementById("myCheck2");
var text = document.getElementById("text1");
var text = document.getElementById("text2");
if (checkBox1.checked == true){
text1.style.display = "block";
} else {
text1.style.display = "none";
}
if (checkBox2.checked == true){
text2.style.display = "block";
} else {
text2.style.display = "none";
}
}
</script>
This is the Html I have for the sidebar and tried an example I found online to print the text but this will print the text on the sidebar bellow the button not on the document.
function onInstall() {
onOpen();
}
function onOpen() {
DocumentApp.getUi()
.createAddonMenu() // Add a new option in the Google Docs Add-ons Menu
.addItem("KO Addon", "showSidebar")
.addToUi(); // Run the showSidebar function when someone clicks the menu
}
function showSidebar() {
var html = HtmlService.createTemplateFromFile("KO")
.evaluate()
.setTitle("KO Addon Options"); // The title shows in the sidebar
DocumentApp.getUi().showSidebar(html);
}
And this is the main code to add the sidebar to the document, I was only able to print things on the document with code on the main but I just don't know how to make them work together, any help would be greatly appreciated,
Thanks.
You want to put the values retrieved from HTML side to Google Document.
If my understanding for your question is correct, how about this sample? In your case, when the values of HTML side send to Google Apps Script, you can achieve it using google.script.run.
Sample script:
HTML side:
Please add the following script to the end of myFunction() at HTML side.
google.script.run.putValues({checkBox1: checkBox1.checked, checkBox2: checkBox2.checked});
GAS side:
Please add the following function to Google Apps Script.
function putValues(obj) {
var body = DocumentApp.getActiveDocument().getBody();
body.appendParagraph(JSON.stringify(obj));
}
Note:
In this sample, it supposes that your script shows the side bar on Google Document and it works fine.
After you modified your script, please open the side bar. When the push button is clicked, the values of check boxed are sent to Google Apps Script and put the values to the active Document.
This is a simple sample script. So please modify it to your situation.
References:
google.script.run
If I misunderstand your question, please tell me. I would like to modify it.
Related
I am working on a form widget on Elementor on WordPress, when the user submits the submit button of the form, it will show a pop-up which is created by elementor. The pop-up actually is a DIV tag including my HTML code which I wrote in an HTML widget into the pop-up, as below, and this DIV will be added by some Elementor function to the body tag of the page dynamically when user presses submit button.
<div id="calenderchooser01" style="min-height:100px;"></div>
<script>
function calenderchooser(){
var calenderhtml="";
var numofunits = document.getElementById("form-field-field_numberunits");
if(numofunits.value>=15){
calenderhtml = "<div class='calendly-inline-widget' style='min-width:320px;width:100%;height:650px;'>over 15</div>";
}else if(numofunits.value<15){
calenderhtml = "<div class='calendly-inline-widget' style='min-width:320px;width:100%;height:650px;'>less than 15</div>";
}
document.getElementById("calenderchooser01").innerHTML = calenderhtml;
console.log(calenderhtml);
}
document.getElementById("thisisanidcustom").addEventListener("click",
calenderchooser
);
</script>
thisisanidcustom is the id of submit button form. Also, I tested it with assigning showing of this pop-up to a normal button.
In both cases when I press the button it shows error on the Chrome console:
Uncaught TypeError: Cannot set property 'innerHTML' of null
at HTMLAnchorElement.calenderchooser (9a48896437a750ba11b18d8323f96ea7.js:59)
it references this line :
document.getElementById("calenderchooser01").innerHTML = calenderhtml;
and I think it says that the div#calenderchooser01 does not exist.
I think maybe the function call occurs before adding the DIV to the body tag. but I have not access to Elementor default functions to edit them and say after popup occurs call the calenderchooser(). so in this situation how can I call the function calenderchooser() right after popups shows up. I also thought of something like an event listener on DIV existing change state if available but found nothing.
this worked for me using window.setTimeout() method:
<div id="over15" class='calendly-inline-widget' style='min-width:320px;width:100%;height:650px;display:none'>over 15</div>
<div id="less15" class='calendly-inline-widget' style='min-width:320px;width:100%;height:650px;display:none'>less than 15</div>
<script>
function functionCall(){
setTimeout(calenderchooser, 100);
}
function calenderchooser(){
var numofunits = document.getElementById("form-field-field_numberunits");
if(numofunits.value>=15){
document.getElementById("over15").style.display = "block";
console.log("over15");
}else if(numofunits.value<15){
document.getElementById("less15").style.display = "block";
console.log("less15");
}
}
document.getElementById("thisisanidcustom").addEventListener("click", functionCall
);
</script>
I'm building a simple webpage.
I wanna select the text inside a <div> and then open a new tab in the same browser and do a Google search for that text with the click of a button. Right now, I just have the solution to copy to clipboard with a button click. Is there any workaround for this...?
I'm OK with using either Google Chrome or Firefox as it's just for a local project. Not meant for public hosting.
UPDATE : I actually need to copy text which is rendered by other HTML code inside the div. I don't wanna copy the code inside the div also.
For reference, here is a code snippet that I used to make my copy to clipboard function.
JavaScript:
function CopyToClipboard(containerid) {
if (document.selection) {
var range = document.body.createTextRange();
range.moveToElementText(document.getElementById(containerid));
range.select().createTextRange();
document.execCommand("copy");
} else if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
document.execCommand("copy");
alert("Copied the text. Use Ctrl+V to paste on Google")
}
}
HTML:
<div class="search" id="div1">
<!--Text to search for (here, CEO of Google)-->
<span>CEO of Google</span>
</div>
<button id="button1" class="button" onclick="CopyToClipboard('div1')">Copy question</button>
This code selects just the text inside the div and then copies it. I don't wanna search for the rest of the code....
Try with the code below.
You can find out more information about how to redirect from your page here.
<html>
<body>
<div id="search_this">New text</div>
<button type="button" onclick="myButtonOnClick()">Search!</button>
</body>
<script>
function myButtonOnClick(){
let url = "https://www.google.com/search?q=";
let searchText = document.getElementById("search_this").innerHTML;
debugger;
window.open(url + searchText);
}
</script>
</html>
This code does the job:
function searchMe() {
var stringQuery = document.getElementById('text_div').innerHTML;
var query = "https://www.google.com/search?q=" + stringQuery.split(' ').join('+');
window.open(query);
}
<div id="text_div" onClick="searchMe();">
Kittens
</div>
Note: Does not work here on stackoverflow but I've tested it in a custom html file and it works.
OK I've got a workaround. Hope it helps someone else with the same problem. A special thanks to both #Daan Seuntjens and #Alex Dragnea for sharing answers as I have used their basic method.
Here, I've selected the text in the <div> and stored it int the range variable which was used earlier for the copy to clipboard function. And then, I did I used another variable query just like both the answers earlier told to add the text https://www.google.com/search?q= to the beginning of the text. And then, I did window.open(query); to open it in another tab and then do a Google search..
NOTE : This code doesn't run here on Stack Overflow. But, I have tested it externally. It is verified and ready to go...
function search(containerid) {
if (window.getSelection) {
var range = document.createRange();
range.selectNode(document.getElementById(containerid));
window.getSelection().addRange(range);
var query = "https://www.google.com/search?q=" + range;
window.open(query);
}
}
<div class="question" id="div1">
<!--Text to search for (here, CEO of Google)....-->
<span>CEO of Google</span>
</div>
<button class="button" onclick="search('div1')">Search</button>
I'm trying to create an add-on on for gSheets to display a sidebar or dialog with a dynamic number of buttons derived from text in a cell which has been clicked on.
There is a varying number of words in the cells of the target column. The objective is to load the words into a single-dimensional array and use each word as the label of a button. Then by clicking on a button, the same text appearing as the button label is copied to the clipboard.
I've successfully gotten the contents of a selected cell to load into an array as follows (the target strings are delimited by a period):
function splitText() {
var array1 = [{}];
var string1 = SpreadsheetApp.getActiveRange().getValue();
array1 = string1.split('. ');
for(var i=0; i<array1.length; i++){
array1[i] = '\\n'+'\\n'+array1[i];
}
Including custom buttons in a sidebar requires referencing an HTML file which I'm doing as follows:
function openSidebar(){
var html = HtmlService.createHtmlOutputFromFile('Sidebar').setTitle('Text Copy Buttons');
SpreadsheetApp.getUi().showSidebar(html);
}
I'm familiar with the basic HTML to create a button as follows:
<body>
<button onclick='myFunction()' id='button1'>LABEL TO BE ASSIGNED</button>
</body>
I'd like to know how to create a variable number of buttons by looping through the array and assigning each array value to a new button label. Any suggestions much appreciated!
You can try jQuery:
Flow:
Serve basic HTML with buttons inside a div
On Window Load run splitText function on server side with google.script.run
Receive the array from splitText and update div with jQuery append
Alternatively, You can directly use Templated HTML to do a for-loop to append.
Index.html:
<head>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script>
$(function(){
google.script.run.withSuccessHandler(showButtons)
.splitText()
});
function showButtons(arr){
var b = $('#buttons');
b.empty();
for (var i=0;i<arr.length;i++){
b.append("<button onclick='myFunction()' id='button"+i+"'>"+arr[i]+"</button>")
}
}
</script>
</head>
<body>
<div id ="buttons">
<button onclick='myFunction()' id='button'>LABEL TO BE ASSIGNED</button>
</div>
</body>
Code.gs:
function splitText() {
return SpreadsheetApp.getActiveRange().getValue()
.toString()
.split('. ');
}
References:
TemplatedHTML
jQuery vs Template
I'm new to XDK, and I'm trying to figure out how to make a button navigate to a random page out of a set group of pages. Is this a Javascript Math.random thing, and if so, how do I apply it to a button in XDK? Any help or pointing to a resource would be helpful! Thanks!
Update: So I've decided to instead have the button generate a random div on a new page. Here's the javascript:
window.onload=function() {
var E = document.getElementsByClassName("item");
var m = E.length;
var n = parseInt(Math.random()*m);
for (var i=m-1;i>=0;i--) {
var e = E[i];
e.style.display='none';
}
E[n].style.display='';
}
And here's the html on the new page:
<body>
<div class="item">One</div>
<div class="item">Two</div>
<div class="item">Three</div>
<div class="item">Four</div>
<div class="item">Five</div>
<div class="item">Six</div>
</body>
The initial load randomizes the div, but the when i go back to the main page and click the button again, it doesn't randomize again. I know the problem is the window.onload, but I'm not sure how to fix it. How do I get the button to reload the array?
On Google sites on edit mode, I have prepared a checkbox using Insert - HTML Box
and within the HTML Box the following code..
<style>
div{
width:100px;
height:30px;
}
</style>
<script>
function putResult(e) {
var ss = SpreadsheetApp.openById("0AkkxdNrvyzqzdE1yU21FRGJ6akJ6MmZiSVhTN0JMNnc");
var calc = ss.getSheetByName("Customer");
var chvalue = e.parameter.bike
calc.getRange("C3").setValue("chvalue");
}
</script>
<div>
EDC:
<input type="checkbox" id="bike" onclick="putResult(e)">
</div>
Now my requirements:
I want simply a True/False based on checkbox to be populated in the SS.Calc (C3) Sheet.
The page should automatically be refreshed each time the checkbox is clicked.
I am a novice and in learning stage. Please do shout if things are unclear.
PS:
I copied some code within GAS, that's where the e.parameter.bike comes from, don't know if that's the right way...
I have also inserted a chart in the Google sites with source data from spreadsheet (insert Chart), I want to make it dynamic using checkboxes.
Old but, since April 2018 they implemented a checkbox in spreadsheets.
You can find it in Menu > insert > checkbox or if you add a datavalidation.
Dealing with your Task by simply checking the field value with
=IF(A1=TRUE();"Checked";"Unchecked")
should be way more easy now! It also instantly checks state changes.
I think HTML Box is not accepting onclick , not sure but by inspecting the checkbox in firebug you can see that there is no onclick for the element.
I think you can do it using google app script as follows,
In code.gs file,
function doGet() {
return HtmlService.createHtmlOutputFromFile('Test').setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
and have file called Test.html.This can be created File --> New --> Script File, in that have the following code,
<style>
div{
width:100px;
height:30px;
}
</style>
<script>
function putResult() {
var ss = SpreadsheetApp.openById("0AkkxdNrvyzqzdE1yU21FRGJ6akJ6MmZiSVhTN0JMNnc");
var calc = ss.getSheetByName("Customer");
var chvalue = document.getElementById('bike').value;
calc.getRange("C3").setValue("chvalue");
}
</script>
<div>
EDC:
<input type="checkbox" id="bike" onclick="putResult()">
</div>
I haven't tested the above code.Try this if it works for you.