Get app_id from google sites - google-apps-script

I want to grant access for my sites by the script, for google docs I can get the id from the url: https://docs.google.com/document/d/<doc_id>/edit
and do this: DriveApp.getFileById('<doc_id>').addViewer(emailid);
but for google sites I get this format instead:
https://sites.google.com/d/<id_1>/p/<id_2>/edit
I tried use id_1 and id_2 but both does not work.
Can anyone help me?
Thank you

The correct File Id to be used for Google Sites is the <id_1>.
The reason why you cannot add viewers to the site is because unlike Google Docs, Google Sites doesn't have a viewer sharing option. Instead, it uses a published viewer option.
Sample: 3 Users with different access rights
(I'm using new Google Sites in this example)
Sample Code:
function myFunction() {
Logger.log(DriveApp.getFileById('id_1').getViewers());
Logger.log(DriveApp.getFileById('id_1').getEditors());
}
Logs:
[21-01-07 00:52:50:404 HKT] []
[21-01-07 00:52:50:719 HKT] [DriveUser]
If you want to set viewers for your Google Site, you can use Sites Service in Apps Script. However, you can only use Site.addViewer(emailAddress) for classic sites.
Note:
A rebuilt version of Sites was launched on November 22, 2016. Apps Script cannot currently access or modify Sites made with this version, but script can still access classic Sites.
(Update)
Currently, there is no other option to update Google Sites via Apps Script or API.
Based on this reference: When will available the API Google Site?
The new version of Google Sites does not offer an API at the moment but Google has announced that API capabilities including Google Apps Script integration will be available.
For more detailed information see Deprecation Timeline - classic Google Sites
Google doesn't offer dates for when features will be available so you won't find one.

Related

Google App Script SitesApp returning an empty array

I tried all methods available in the Google Sites API, examples
var site = SitesApp.getSite("example.com", "mysite");
the above method returns an empty array.
And the bellow method returns an error with "Invalid argument: url "
var site = SitesApp.getSiteByUrl("https://sites.google.com/domain/testweb");
How to read a Google Site properly using Google App Script ?
Details:
I own the site and the site is a new Google site NOT a classic one.
As written in the documentation,
A rebuilt version of Sites was launched on November 22, 2016. Apps Script cannot currently access or modify Sites made with this version, but script can still access classic Sites.
So, you cannot access new sites with apps script.
Users who want to access new sites through apps script can add a star(★ on top left) to this feature request to let Google know you're interested and for Google to prioritize the issue.

One question survey app Google Apps Script and Google Sheets with user management

I have to do a little bit of "crowdsourcing" for my work and it would consist in a very simple web app where a user can register/log in, and then be taken to a page where a picture is shown to a user and submit a number.
I'd like to be able to set up a few rules to choose which picture the user will see (so he won't have to answer twice the same question and also to allow some overlap between users to compare their answers).
We can assume that I have a google sheet with a list of images URLs that can be accessed and that I would like the answers to be populated there.
I figured that this tutorial would be a good start since it handles user creation and management and user can submit ideas :
Creating a CRUD Web App with Google Sheets
That being said I'm a bit clueless, I've tried to look for sample scripts deployed as web app but it's hard to know where to start.
I'll appreciate any help !
Google Apps Script is, basically, a javascript environment with a set of libraries that interact with the Google Apps. For instance, to read or store information from/to Google Sheets or Google Docs. If you know javascript, you can create simple applications there. In addition, you can create plugins for Google Applications if you are interested.
A very simple example
Suppose you wanna display an HTML page. You can create an stand-alone script (an script not-bound to a G suite application) and use the content or the HTML services.
You can create a function that sends a simple message to the browser using the ContentService:
function doGet() {
return ContentService.createTextOutput('Hello, world!');
}
Or you can create a function that sends an HTML file in the same project using the HtmlService:
function doGet() {
return HtmlService.createHtmlOutputFromFile('Index');
}
Once you have created the function, you must deploy the script as a web application.
Save the script as a new version doing File > Manage Versions and Save new version.
Then, publish the app using Publish > Deploy as web app and provinding information about the permissions for the application.
After Google publishes the application, it gives you an URL to access the application,
You may check more in a simple tutorial on producing content with Google Apps Script. In addition, you may check the google guide to use the HtmlService to provide templated content, i.e. where the HTML are templates which data is provided by javascript functions and variables.
Getting started with Google Apps Script
To start, you may check some tutorials in the internet:
Google has some tutorials and a series of Youtube videos.
In addition, you may find multiple resources for Google App Scripts that may help you:
Google has a list of sample projects
Tanakeich has a list of resources for taking advantage of Google Apps Script.
Oshliaer has another list of resources.
There are Google Codelab (tutorials) for Apps Scripts. They include a lab for using Google Sheets as a reporting tool and for creating a Hangouts Chat bot with Apps Script
Developing Google Apps Script projects locally
Although Google Apps Script provides a web interface to create your applications (your scripts), a better idea may be to create the software locally, using more-traditional tools for Javascript.
Clasp is a command-line tool to develop locally Google Apps Script projects.
Google has a codelab that teach you how to use clasp.

Chrome app and connecting to google doc

I want to start looking at using chrome apps as a possible IT solution. I used this tutorial to create a quick app:
https://developer.chrome.com/apps/app_codelab_basics
My next step is to connect to an external data source, where multiple users could access (read/write) to the same source. Is it possible to read/write to the following, and if so, are there any tutorials:
Google Sheet, stored on google drive
An access db saved in google drive
An Excel file saved on google drive.
Thanks
Have you, by any chance, tried using Google Apps Script for this.
Here's a simple tutorial:
https://zapier.com/blog/google-apps-script-tutorial/
Here's the complete API documentation (small examples included):
https://developers.google.com/apps-script/reference/spreadsheet/
I have configured fairly complex workflows by just using Google Apps Script with various Google Apps.

Summary Charts in Google Script from Google Spreadsheet

I'm new to Google scripting and have a very basic dashboard put together from all the information available online. The source is a Google spreadsheet of data exported from a database. I'm creating a Dashboard web app and would like to create summary charts on the dashboard due to the amount of information I'm working with. I've contemplated using Pivot Tables but would then lose the functionality of the CategoryFilters. I've seen lots of stuff about Google Visualization online and the Group function, but this doesn't seem to work in Google script. Can anyone advise what to do? Thanks.
Google give API for Google app script and Javascript.
It's really different because in app script you use a script in your google drive, and you use an app script editor.
If you use Google Vizualisation it's only javascript , you can deploy your javascript application in google app engine for example.
Google vizualisation is very interesting if you want to create a dataTable , a map , or others.
I use Google vizualisation for many charts because it's more simple and more dynamic than google app script.

How can developers edit a Google Doc programmatically? Is there a Docs API?

There doesn't seem to be (to my knowledge) an API to edit Google Docs (not spreadsheets, their HTML based documents). Has anyone done something like the? Maybe by downloading the HTML version, editing and uploading the changes?
UPDATE (May 2019) The Google Docs API was officially launched in Feb 2019. The documentation is located at the link from my update in July below. A few weeks after launch, I produced a high-level video overview of what a mail merge application using the API would look like. (It's not a full-fledged G Suite Dev Show episode but does link to a working sample.)
UPDATE (Jul 2018) The Google Docs team pre-announced a forthcoming REST API at Google Cloud NEXT '18. Developers interested in getting into the early access program for the new API should register at https://developers.google.com/docs. The original answer below still stands as the REST API will become the second way you can access Google Docs programmatically.
Original answer (Mar 2017): (Most other answers are outdated.) Google Docs does not currently have a REST API, however developers can programmatically access (CRUD) documents using Google Apps Script, server-side JavaScript apps that are hosted at and run in Google's cloud. If you're new to Apps Script or to editing Google Docs with it, here are some learning resources:
Your first script which creates & edits a Doc, then uses Gmail to send it
to you.
I've got 4 intro videos for you (mostly Sheets-flavored)
They're in this playlist (see videos 5, 8, 22, 24)
Any forthcoming videos will be in this series
Useful pages in the official docs
How to CRUD Google Docs with Apps Script overview page
Extend Docs functionality by creating Docs add-ons via this quickstart
Apps Script reference documentation for Google Docs (Document Service)
See Google Docs add-ons that other developers have built
Simple example: if you have an existing Doc with a (Drive) file ID of DOCUMENT_ID_GOES_HERE, here's how you'd basically edit it with Apps Script, doing a pseudo "mail merge" of name & email into the document given placeholders {NAME} and {ADDR}:
function mergeNameEmail() {
// Open a document by ID
var doc = DocumentApp.openById(DOCUMENT_ID_GOES_HERE);
// Access the body of the document
var body = doc.getBody();
// Merge name & address from template
body.replaceText("{NAME}", "Ima Developer");
body.replaceText("{ADDR}", "123 Main St, Anytown, XX 00000");
}
The Document List API has been deprecated since September 2012 and looks like it could be retired after April 2015.
Updating the HTML version using the Drive API, as the question suggests, looks to be the only other way. I have been trying this and I have experienced a few of issues.
Comments are converted into citations and added to end of document.
If someone else is editing the doc via the browser any changes made by them between the API read and update time are lost.
Updates to a doc can break the formatting. For example I updated a doc several times and the vertical spacing between some elements (h1's, h2's etc) kept widening each time and ruined the doc.
When an API update occurs the cursor of anyone in the doc is moved to the top of the page.
There may be more issues. These are just the ones I have found in the last few days.
Not really sure if this is what you're looking for exactly but have you taken a look here http://code.google.com/apis/documents/docs/3.0/developers_guide_protocol.html It looks like it allows editing for content (v3.0 anyway).
There is com.google.api.services.drive.model.File.getExportLinks
You can get a Google Doc as a docx (for example), edit it using your favourite docx editor, then upload again. See the samples for doing this (starting with GoogleDriveDownloadAsDocx) in the context of docx4j. Note the README.
Or do the same with any of the other export formats.
(2019) Google now provides API for docs, slides, sheets, drive.
There is a sample app for this, Dr. Edit, on Google Drive's documentation.