Disable comments when creating a page from script (Google Apps Script) - google-apps-script

I'm creating a simple child page in a Google site using Apps Script.
function createLibraryEntry(sessionObject){
var site = SitesApp.getSite("student.vis.ac.at", "professional-development-library");
var library = site.getChildByName('library');
var sessionPage = library.createWebPage(
"session1234",
"session1234",
"<h1>Session 1234</h1>"
);
}
Having read the documentation and done a fair amount of searching I'm unable to find a way to disable comments or attachments, on creation, for the new page. Neither can I find a way to set them on the page, by script, after creation (this can be achieved in the sites UI through 'page settings').
Can anyone help with this please?

Related

Control Multiple Google Sheets through a common Library instead of code.gs [duplicate]

I have a google apps script that I want to use in multiple documents. I also may want to change it later in those documents, so it is imperative that I use the same script in all those documents, and not copies of that script.
I am aware of the below question, which may qualify as a duplicate, but I am reluctant to accept its answer.
Google Apps Script - How To Have One Script In multiple containers?
So, my question is: is there really no way to share a script among multiple google documents? Must you really create a new script project for every document and copy-and-paste the code from an other? Moreover, if you fix a bug in one of them, do you have to remember which documents use that script and open the script editor in each of them and copy-and-paste the new code?
Libraries were designed specifically for this situation... please have a look at the documentation here.
I have come up with a solution that works for me. It allows keeping any number of scripts attached to a sort of master document (let's call it MyScripts). No libraries, no publishing required.
Create a document and name it MyScripts (or whatever). The document's body can be empty, or you could write some instructions there. Then, paste the following code into MyScript's script editor:
// adds a menu to the master document's UI
function onOpen() {
DocumentApp.getUi()
.createAddonMenu()
.addItem('Do something', 'doSomething')
.addItem('Something else', 'somethingElse')
.addToUi()
}
// returns the target document based on its URL
// may be tweaked in order to use the documentId instead
function findDoc(prompt) {
var ui = DocumentApp.getUi();
var pro = ui.prompt(prompt, 'Document URL:', ui.ButtonSet.OK);
var url = pro.getResponseText();
return DocumentApp.openByUrl(url);
}
function doSomething() {
var doc = findDoc('Do something');
if (doc) {
// do something with the target document
var len = doc.getBody().getText().length;
DocumentApp.getUi().alert('The document is ' + len + ' characters long')
}
}
function somethingElse() {
var doc = findDoc('Something else');
if (doc) {
// do something else
}
}
The onOpen() function should be self explanatory.
findDoc() is the real hack. It prompts the user for the URL of the target document, the document we want to act on. If the URL is valid, then findDoc() returns the corresponding document object.
The last two functions are just stubs and should be replaced with your own code, but notice how findDoc() gets called at the beginning of each.
When you want to run a script against a document, copy its URL, then open MyScripts, choose the corresponding Add-Ons menu item, paste the URL and click OK.
Please notice that you will get a scary warning message the first time you attempt to run a script this way. Just be sure that your doSomething(), your somethingElse(), etc. only contain safe code before ignoring the warnings and executing the scripts.
As of September 6, 2020, using a library implies to create a project to add the reference to the library and some code to make the library functions available to the container document. The only way to have a script available on multiple documents without having to create an script on them and without limitations is by creating a G Suite Editor add-on.
As of December 2019 all the G Suite editor add-ons are published to the G Suite Marketplace. You could make an add-on unlisted and only users having G Suite accounts could published add-ons limited to be visible by other users from the same domain.
Test as add-on
If you don't want to publish and add-on you might use the Run > Test as add-on but there are several limitations. I.E. the following can't be used on this mode:
Triggers
Custom Functions
Master project
As suggested on Giussepes' answer, is to use a "master project" to hold your scripts. This also has several limitations
Most of the active methods can't be used
Simple triggers can't be used but it's possible to programatically create installable triggers
Custom functions only works on spreadsheets add-ons and scripts bounded to the spreadsheet that will use the custom function
Be resigned
If you are resigned to have a project on each of your documents you could reduce the burden of keeping the scripts updated by using CLASP or the Goole Apps Script Assistant for GitHub
Resources
Builting editor add-ons | G Suite
Command line interface using clasp | Google Apps Script
Sorry, my reputation was too low to add a comment. :(
In Giuseppe's answer use this for find docs - minor change
function findDoc(pros) {
var ui = DocumentApp.getUi();
var pro = ui.prompt(pros, 'URL:', ui.ButtonSet.OK);
var url = pro.getResponseText();
return DocumentApp.openByUrl(url);
}

Google Picker shows up blank when used in Google Sites

To all Google Apps experts - please help me solve this. I'm rather stuck and I've not found any explanation yet on why this problem exists at all. I've included a live example to demonstrate the problem.
The problem may be view at this location ...
https://sites.google.com/a/growthhq.net/faulty-picker/
A Google Apps script made available in a Google Sites using the Apps Script gadget has stopped functioning recently. The crazy thing is that the app works perfectly using the 'dev' and 'exec' urls for the app directly, but when embedding the app in Sites (with the 'exec' url and Apps Script gadget), and the button is clicked, the form comes up blank.
I know that the UiApp is deprecated (sadly, this had some good attributes) but I don't want to change the rest of the code at this stage.
What can I do to get the Picker to show correctly in sites? Is the referrer inadequately specified? I'm at a total loss.
The activated referrers are:
*.google.com
*.googleusercontent.com
I have extracted (and simplified) the code portions for testing purposes. This working test code follows:
// refer to https://developers.google.com/apps-script/guides/dialogs#file-open_dialogs for setting up OAuth
function doGet()
{
var app = UiApp.createApplication();
var buttonHandler = app.createServerHandler('utilityPicker');
var button = app.createButton('Open Picker', buttonHandler)
app.add(button);
return app;
// This is a dummy to activate
DriveApp.getRootFolder();
}
function utilityPicker(e){
var app = UiApp.getActiveApplication();
var authToken = ScriptApp.getOAuthToken();
Logger.log(authToken);
var docPicker = app
.createDocsListDialog()
.setOAuthToken(authToken)
.setDialogTitle('Select a Google Spreadsheet or Form to be used by this Workspace')
.setMultiSelectEnabled(true)
.addView(UiApp.FileType.FOLDERS)
.showDocsPicker()
;
Logger.log(docPicker);
docPicker.addView(UiApp.FileType.SPREADSHEETS);
var handler = app.createServerHandler('pickerPrimarySpreadsheet');
docPicker.addSelectionHandler(handler);
return app;
}
function pickerPrimarySpreadsheet(e)
{
Logger.log(e.parameter);
}
Please help.
got the same problem,
found some google group forum where some people had to
add
.setOrigin('https://script.google.com')
to the pickerbuilder
still does not solve my problem, if u got a working solution to use on google sites please share

edit google document remotely

I'm trying to add a feature to my website as follows:
Clicking a button appends text to a Google document.
Obviously I will need to create an Apps Script in the drive. The question is how to trigger the Apps Script from my website. You can assume that I am the owner of the drive/document and so have permissions to edit it in any way I like.
I have looked at these topics:
Workarounds : How to edit native google documents programatcally?item
How to programmatically manipulate native google doc files
It seems they are all actions performed from the drive itself and not triggered remotely.
I have also looked up the Apps Script API in Google but have not found a way to do this. Is it even possible?
Yes, it is possible.
First write an Apps script that changes your desired document. Then deploy it as a web-app running as you that anyone has access, even anonymous. Check out the guides at Apps Script page to see how to write your script as a web-app.
Your script will then have a public url, which you can call from your website and have it run "remotely" normally.
To provide an example of what Henrique suggests, here is a small webapp that adds text to a publicly viewable document I own (it doesn't need to be public except for anyone here to check it works !)
I wrote it using UiApp but you could of course use HTMLService if you prefer...
The app runs as me but is accessible to anyone even anonymous.
// publicly viewable test doc url : https://docs.google.com/document/d/1THzBTURxGr2CdUmcZ7i2zD-RM8I3im2JCSHI3BHlkeM/edit
function doGet(){
var app = UiApp.createApplication().setTitle('docEdit');
var panel = app.createAbsolutePanel().setSize('100%','100%').setStyleAttributes({'padding':'40px','backgroundColor':'lightBlue'});
var text = app.createTextArea().setName('text').setPixelSize(500,300);
var grid = app.createFlexTable().setId('grid');
grid.setText(0,0,'Add your text').setWidget(1,0,text);
var handler = app.createServerHandler('writeText').addCallbackElement(panel);
grid.setWidget(2,0,app.createButton('update document',handler).setId('btn'));
app.add(panel.add(grid));
return app;
}
function writeText(e){
var doc = DocumentApp.openById('1THzBTURxGr2CdUmcZ7i2zD-RM8I3im2JCSHI3BHlkeM');
var now = Utilities.formatDate(new Date(),Session.getScriptTimeZone(),'MMM/dd/yyyy # hh:mm:ss');
var body = doc.getBody();
body.appendParagraph('Append text on '+now+' : '+e.parameter.text);
doc.saveAndClose();
var app = UiApp.getActiveApplication();
var grid = app.getElementById('grid');
grid.setWidget(3,0,app.createHTML('Thanks,<br>Your text has been added to the document'));
app.getElementById('btn').setEnabled(false).setHTML('Button disabled');
return app;
}

How to pull window`s login id in google apps script

I am using a google apps script and writing a script for my google spreadsheet. I need to take the windows login id so that I can know who is filling up the spreadsheet.
All my searches in the web yield the code that looks like this:
var wshell = new ActiveXObject("WScript.Shell");
alert(wshell.ExpandEnvironmentStrings("%USERNAME%"));
But google apps throws an error saying ActiveX not found. I am new to JavaScript and Google Apps.
Thanks a lot.
This is not possible nor should it be as it would be a huge security issue.
Explains more: JavaScript - How to get the name of the current user
Reason it doesn't work on windows in explorer is because your google-apps-script code get sanitized by google-caja.

Creating a Mobile User Interface for Google Spreadsheet

Is it possible to make an HTML interface for a spreadsheet that doesn't run inside the spreadsheet? Basically I want to use the spreadsheet as a simple database.
I can't seem to find a way to do it in the documentation. I got this to work this way:
var ss = SpreadsheetApp.getActive();
function onOpen() {
var html = HtmlService.createHtmlOutputFromFile('index');
ss.show(html);
That opens my page automatically when I load the sheet, which is not a bad way to have it work, but I would rather run it from a separate page without having to know it is looking at a spreadsheet.
Also, this script doesn't work on mobile browsers which is an issue.
Is what I want to do possible currently? I have been looking at the documentation for a while without a clear answer.
I believe you will be wanting to deploy your script as a web app, rather than a "container-bound" script inside a spreadsheet.
As there will be no spreadsheet inherently associated with the web app, you would need to use the openById() method rather than getActive().