Share to classroom button stopped functioning - google-apps-script

I have inserted instances of the Share to Classroom button on my school GSite using GAS.
Html:
<script src="https://apis.google.com/js/platform.js" async defer>
</script>
<? var site = SitesApp.getSite("domain", "webiste name"); ?>
<? var page = SitesApp.getActivePage().getUrl(); ?>
<div style="height: 35px; ">
<g:sharetoclassroom size=32 url="<?!= page ?>"></g:sharetoclassroom>
</div>
Code.gs:
function doGet() {
return HtmlService
.createTemplateFromFile('gClassroomShare')
.evaluate()
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
The web app is published to run as me for anyone in the domain.
They have been working perfectly up until yesterday/today. When I click on the buttons now, the new empty window opens with about:blank in the url.
Any idea what would have happened to break it? Would a setting in the Admin Console block this?
Cheers.

Related

Getting the "You do not have permission to call getFolderByID" error, when calling that method in HTML

I am going through the book Google Apps Scripts, 2nd Edition. The lesson I am on for creating a web app asked me to use the following code:
function doGet() {
var html = HtmlService.createTemplateFromFile('index').evaluate()
.setTitle('06 Automating Forms')
.setSandboxMode(HtmlService.SandboxMode.NATIVE);
return html;
}
However, when I run this code I get the "You do not have permission to call getFolderById" error message. I don't understand why I am getting this error message. Also, it says the error is on line 2.
I am calling the "getFolderByID" method in my index.html file. Here is that code:
<div class="body">
<div><h2>This App will allow you to create a form from a template
in Google Docs.</h2></div>
<hr>
<div id="options">
<?var files =
DriveApp.getFolderById('0B6YzuBeNxooXSWE3Vm9WMlVnWkk').getFiles();?>
<select id='template'>
<option value="notSel">Select a Template</option>
<?while (files.hasNext()){
var file = files.next();?>
<option value="<?=file.getId()?>"><?=file.getName()?></option>
<?}?>
</select>
</div>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
</script>
<script>
</script>
<style>
.body{
padding: 10px;
}
</style>
Is there a way to get permission to call that method? I thought if I run the code a box pops up and allowing me access to that area of Drive.
Only script files have the ability to ask for authorization, html files don't.
The simplest thing to do is to add a dummy function in your code.gs file like this :
function myFunction() {
DriveApp.getFolderById('0B6YzuBeNxooXSWE3Vm9WMlVnWkk');
}
run it from the script editor and you will get the authorization request as expected.

Submit button on html service called Google form not working

This function in a google sheet does popup a window with the form, but the submit button is dead. What is missing that will make this window a live one?
function launchForm() {
var formID = '1YznrnFFIfXLpsZfsIZ4xjhlwmi8eLv_nTuOjDKGhwIc';
var form = FormApp.openById(formID);
var formUrl = form.getPublishedUrl();
var response = UrlFetchApp.fetch(formUrl);
var formHtml = response.getContentText();
var htmlApp = HtmlService
.createHtmlOutput(formHtml)
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Ta Daaa!')
.setWidth(500)
.setHeight(650);
SpreadsheetApp.getUi().showModalDialog(htmlApp, 'Query window');
}
The function is hooked to a menu item and clicking on that item does bring up the form over the sheet, as does running the script from the script project attached to the sheet.
menuEntries.push({name: "Launch Query form", functionName: "launchForm"});
This particular form is simple, only three fields. So I could consider using HTML service to build an HTML and I see lots of questions about that not working either, so I can probably work that out. However, I have other forms that are a lot more complex and being able to simply call a regular Google form from within the spreadsheet is what I'd rather do.
Embed the URL of the Google Form into the HTML of the dialog box:
Code.gs
function launchForm() {
var htmlApp = HtmlService
.createHtmlOutputFromFile('InputForm')
.setSandboxMode(HtmlService.SandboxMode.IFRAME)
.setTitle('Ta Daaa!')
.setWidth(500)
.setHeight(650);
SpreadsheetApp.getUi().showModalDialog(htmlApp, 'Query window');
}
InputForm.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<iframe src="https://docs.google.com/forms/d/e/FormIDHere/viewform?embedded=true" width="760" height="500" frameborder="0" marginheight="0" marginwidth="0">Loading...</iframe>
</body>
</html>
In "New" Google Forms you can get the IFRAME tag to embed the Google Form by clicking the "Send" button at the top right, and then clicking the third tab "<>"

Close dialog when both dialog and custom sidebar are present

I'm having trouble with the google.script.host.close() command in that it is closing my sidebar rather than the dialog.
function clickWeek() {
setWeekColor('week', '#7FFF00');
setMonthColor('month', '#d6d6c2');
setPressColor('press', '#d6d6c2');
setAllDataColor('allData', '#d6d6c2');
google.script.run.withSuccessHandler(google.script.host.close).weekAheadCreate();
}
The dialog is opened from the top of the weekAheadCreate() fucntion as per below:
var htmlOutput = HtmlService
.createHtmlOutput('<p>Please wait a moment...</p>')
.setWidth(250)
.setHeight(80);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Loading');
Any ideas how I can force the google.script.host.close() to act on the dialog rather than the sidebar?
The close() method closes the current dialog/sidebar, and asuming the clickWeek() is in your sidebar, it will close it instead of the dialog. You need to run the close() method inside the dialog.
How about you fire your server-side function when the dialog is created and when the withSuccessHandler() detects a successful return then it closes the dialog using close().
You will need to use createHtmlOutputFromFile when creating the dialog insted of createHtmlOutput and inside your html use the <script> tag. Here's an example:
code.gs
function createDialog(){
var htmlOutput = HtmlService
.createHtmlOutputFromFile('dialog')
.setWidth(250)
.setHeight(80);
SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Loading');
}
function doBusyStuff(){
// Busy stuff
}
dialog.html
<!DOCTYPE html>
<html>
<head>
<base target="_top">
<script>
(function() {
// Runs the busy stuff and closes the dialog using .close() on success
google.script.run
.withSuccessHandler(google.script.host.close)
.doBusyStuff();
})();
</script>
</head>
<body>
<p>Please wait a moment...</p>
</body>
</html>

Opening link in new tab without clicking on link

I'm currently (in HTML) trying to load a link in a new tab or window right when the website is opened, without anyone clicking on a link on a page. I've so far managed to open a link automatically and open a link in a new tab and window, but not at the same time. Can someone help me with this? I also don't mind using another language if this is not possible in HTML.
First question why do you want to do that?
Secondly You can use javascript for that.
function OpenInNewTab(url) {
var win = window.open(url, '_blank');
win.focus();
}
And in your HTML put
<body onload=OpenInNewtab('http.....')>
.......
</body>
Here is the code to open a link in new tab on page load using jquery.
<html>
<head>
<script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
<script>
jQuery(document).ready(function () {
newTab();
});
function newTab() {
var form = document.createElement("form");
form.method = "GET";
form.action = "http://www.google.com";
form.target = "_blank";
document.body.appendChild(form);
form.submit();
}
</script>
</head>
<body>
<a class="my-link">link</a>
</body>
</html>
If you want to open a new page in new window once the main page of the website is loaded, try this by calling the onload javascript function in the body:
<body onload="myfunction()">
And then in myfunction() you can call try this !
window.open(url, '_blank');

"getCalendarById" is not defined

What is wrong with my Code.gs that gives me "getCalendarById" is not defined? Also If I add CalendarApp. in front of the getCalendarById method the whole thing breaks and the URL returns nothing but a drive error.
Solution:
I do have to add CalendarApp in front of getCalendarById I read the doc wrong (doc link). In that doc under the section 'Granting Access Rights' I read -
Scripts do not request authorization if they...or if you access the script as a web app that runs under the script owner's user identity.
I was running the script under my own identity but failed to realize that the quote above meant it will not go out and get authorization, not that it still requires you to get it yourself . Many thanks to Serge insas for his great help, sticking with me and understanding.
Code.gs:
function doGet() {
return HtmlService.createHtmlOutputFromFile('index.html').setSandboxMode(HtmlService.SandboxMode.NATIVE);
}
function listEvents(dateSelected) {
var calendar = getCalendarById('en.usa#holiday#group.v.calendar.google.com').getEventsForDay(new Date(dateSelected)); // if I put calendarapp. the whole thing returns an error from the URL
Logger.log('Number of events: ' + calendar.length);
}
html:
<link rel="stylesheet" href="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/themes/redmond/jquery-ui.css">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="//ajax.googleapis.com/ajax/libs/jqueryui/1.9.1/jquery-ui.min.js"></script>
<div class="container" >
<div>
<h1>Welcome</h1>
<p>Please select a date below.</p>
<p>Click Here: <input type="text" name="date" id="datepicker" /><p>
</div>
</div>
<div id='test'></div>
<script>
$("#datepicker").datepicker();
$("#datepicker").on("change", function () {
var dateSelected = $(this).val()
$("#test").text(dateSelected);
google.script.run.listEvents(dateSelected);
});
</script>
getCalendarById() is a method of the calendarApp service, the correct syntax is as follows :
var calendar = CalendarApp.getCalendarById("xxxxxxxx");
See doc here