Share a Drive document without notifying user with Google Apps Script - javascript

I am creating a workflow process in Apps Script where a Doc is generated from a template and shared with various users for approval. The Script sends a customised email notifying a user that the document requires their approval but they also receive a second email at each stage in the process from the user whose Drive the document is stored in saying "User has shared a document with you". Is there any way of disabling these alerts? When you share a document manually from your Drive, there is a checkbox option that allows you to choose whether or not the user receives a notification. However, I cannot find a way to disable this notification with Apps Script.
I am using doc.addEditors(users) to share the document.
Many Thanks

Another option would be to use the Drive advanced service (which you should enable in the Resources menu in the Script Editor).
The code used should be
Drive.Permissions.insert(
{
'role': 'writer',
'type': 'user',
'value': 'bob#example.com'
},
fileId,
{
'sendNotificationEmails': 'false'
});

There is a simple solution if you are working with Google Docs or Google SpreadSheets. You can use DocumentApp or SpreadSheetApp to share your Docs or SpreadSheets without email notification:
DocumentApp
var doc = DocumentApp.openById('124144')
doc.addEditor('example#mail.com').addViewer('example2#mail.com')
SpreadSheetApp
var spreadSheet = SpreadsheetApp.openById('124144')
spreadSheet.addEditor('example#mail.com').addViewer('example2#mail.com')
However, if you are working with documents that aren't Docs or SpreadSheets, you must share then using DriveApp and email notification will be send.

This isn't possible at the moment. More information about this topic can be found here: https://code.google.com/p/google-apps-script-issues/issues/detail?id=2829
A workaround suggested in the comments of the above issue is to use DocsList instead:
DocsList, SpreadsheetApp and DocumentApp have addEditor and addViewer methods that do not result in notification emails.

Related

How to track user clicks on Phaser web-game?

So I've made a web game with the Phaser.js framework. Without a database, I want to track events/actions in the game. The data consist of at least two strings: time_stamp & event (i.e. "click the 'Play' button").
My current attempt is to write the data to my google sheets. How to do that?
I want something like:
Function for 'Play' button
playGame() {
// Go to gameplay scene
scene.start(Gameplay)
// Script to insert data to google sheet
submitEvent(timestamp, "click the 'Play' button"){
}
}
Or maybe there's an easier way to do the tracking? Need to be a free service and capable to store data online with javascript.
I tried the Google Sheet API but was confused with the doc, it seems like I need the player to log in and authorize, is it possible to avoid that? Most tutorials use HTML Form to send data, which I didn't use.
I believe your goal is as follows.
You want to put the values of timestamp and "click the 'Play' button" to your Google Spreadsheet using Javascript without authorization process.
In the current stage, in order to put a value to Spreadsheet using Sheets API, it is required to use the access token retrieved from OAuth2 and the service account. I'm worried that in this case, the script might be a bit complicated, and it might not your expected direction.
If you want to put a value to your Spreadsheet with a simple script without the authorization process, how about using Web Apps created by Google Apps Script? When Web Apps is used, your goal might be able to be achieved. When the method for putting a value to Spread sheet with Web Apps is implemented in Javascript, please do the following flow.
Usage:
1. Create Spreadsheet.
Please create Spreadsheet. You can create a new Spreadsheet by accessing https://docs.google.com/spreadsheets/create. In this case, the Spreadsheet is created in the root folder of your Google Drive. And please open the script editor on Spreadsheet.
2. Prepare Google Apps Script.
Please copy and paste the following script to the script editor and save the script.
function doGet(e) {
const { value1, value2 } = e.parameter;
SpreadsheetApp.getActiveSpreadsheet().getSheets()[0].appendRow([value1, value2]);
return ContentService.createTextOutput("Done.");
}
3. Deploy Web Apps.
The detailed information can be seen in the official document.
Please set this using the new IDE of the script editor.
On the script editor, at the top right of the script editor, please click "click Deploy" -> "New deployment".
Please click "Select type" -> "Web App".
Please input the information about the Web App in the fields under "Deployment configuration".
Please select "Me" for "Execute as".
Please select "Anyone" for "Who has access".
Please click "Deploy" button.
When "The Web App requires you to authorize access to your data." is shown, please click "Authorize access" button. And, please authorize the scopes.
This authorization process is only one time. When you access Web Apps using Javascript, no authorization is required, because it has already been done here. I thought that this might be an important point to your expected direction.
Copy the URL of the Web App. It's like https://script.google.com/macros/s/###/exec. This URL is used for your HTML.
4. Testing.
When the values of value1 and value2 are timestamp and "click the 'Play' button", the sample script of Javascript is as follows. And, please set your Web Apps URL. And, please use this script in your script.
function sample(value1, value2) {
const webAppsURL = "https://script.google.com/macros/s/###/exec"; // Please set your Web Apps URL.
fetch(`${webAppsURL}?value1=${value1}&value2=${value2}`).then(async (res) => console.log(await res.text()));
}
When you run sample(value1, value2) by giving the values, the values are put to the 1st sheet of the Spreadsheet.
Note:
When you modified the Google Apps Script of Web Apps, please modify the deployment as a new version. By this, the modified script is reflected in Web Apps. Please be careful about this.
You can see the detail of this in my report "Redeploying Web Apps without Changing URL of Web Apps for new IDE (Author: me)".
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script (Author: me)

Get Google Ads customer id via login

I use “google-ads-api” to manage google ads. And api needs “customer_account_id” to initialization. However, I Couldn’t found how to get “customer_account_id” programmatically from user.
I used “react-google-login” in front-end, but there is no “customer_account_id” in the response. What is the best way to get customer_account_id?
You'll have to use the CustomerService's ListAccessibleCustomers method. This request does not require a customer_account_id and you can use it to obtain all Google Ads account IDs that the authenticated user has access to.
See the documentation for details.
I believe that in google-ads-api the relevant class is called AccessibleCustomersService.

Google AdminDirectory API - Accessing license info and custom schemas

So I have a piece of Apps Script that works as expected - but there are 2 things I can't for the life of me figure out.
We have a custom schema, which is customSchemas.additionalAttributes.division - I can't figure out how to pull this from the AdminDirectory API.
Also - when performing a full user extract using Google Apps Manager (GAM), it pulls user license information - but again, it's not something that I can see when I look at the documentation for the API.
Does anyone know how to pull the custom schema info and the license data?
Link to documentation: Workspace Admin SDK > Directory API
You can pull the customSchemas of a user with the method Users:get
Set fields to customSchemas
Important: set projection to full
Keep in mind that this mehtod will only return you customeSchemas for a given user if a value for the particular schema has been assigned to the particular user
Sample response:
{
"customSchemas": {
"additionalAttributes": {
"division": "testdivison"
}
}
}

Determine if current user has write access for google sheet

I am using Google Sheets API from a Javascript application.
Users log in to my app and grant it permission read/write permission to google sheets.
I have hardcoded a document ID for a google sheet into my application.
I want to be able to make a rest query to determine the current users access rights to that particular document. The user may have:
no access
read only access
read write access
(I manually share the relevant access to the document to each user)
I am looking for a way of doing this. I have searched the sheets API and the drive API but I can't find any call.
At the moment I plan to create a function which will:
try and read the document - if I get an error return "NO ACCESS"
try and write the document - if I get an error return "READ ONLY"
return "READ WRITE"
It seems like a bad idea to determine write access by actually making a write to the document.
Does anyone have any better ideas?
I have tried listing the permissions using this API:
https://developers.google.com/drive/v3/reference/permissions/list
but I just get a list of permissions and this has to be parsed. Also my test read only user got forbidden.

How to allow users to see my Google Analytics chart data without undergoing authentication/Sign In process

I am using the Google Analytics Javascript library to let users view a GeoMap of the particular page they are on. However, everytime they attempt to do so, thye have to go through an authentication process only to have my data displayed on my page. How can I find an alternative to this. I only want to embed my Analytics data through a visualized graph on my page so that all anonymous viewers can see it. This is the code where the process takes place.
google.setOnLoadCallback(init);
/**
* This is called once the Google Data JavaScript library has been loaded.
* It creates a new AnalyticsService object, adds a click handler to the
* authentication button and updates the button text depending on the status.
*/
function init()
{
myService = new google.gdata.analytics.AnalyticsService('charts_sample');
/*
this is essentially the line I want to replace. It anonymously
tries to access my viewer's account.I want to set this scope such as it
address the URI of my Analytics account
*/
scope = 'https://www.google.com/analytics/feeds';
/*
if the above scope is altered to my URI then the below unnecessary login
steps will not be required at all. This is the reason why my viewers are
taken to their analytics page which is fundamentally non-existent.
*/
$("authButton").onclick = function() {
// Test if the user is not authenticated.
if (!google.accounts.user.checkLogin(scope))
{
// Authenticate the user.
google.accounts.user.login(scope);
} else
{
// Log the user out.
google.accounts.user.logout();
getStatus();
}
}
getStatus();
}
// gets the account data and then gets the outputs of the requested query
function getStatus()
{
getAccountFeed();
getDataFeed();
}
function getAccountFeed()
{
//Here agsain, I want to modify it access my account's URI
var myFeedUri ='https://www.google.com/analytics/feeds/accounts/default?max-results=50';
myService.getAccountFeed(myFeedUri, handleAccountFeed, handleError);
}
function getDataFeed()
{
var myMapPopularityFeedUri = 'https://www.google.com/analytics/feeds/data?ids=ga%3A53482361&dimensions=ga%3ApagePath%2Cga%3Acity%2Cga%3Acountry%2Cga%3Aregion%2Cga%3Alongitude%2Cga%3Alatitude&metrics=ga%3Avisitors&start-date=2011-12-01&end-date=2011-12-25&max-results=50';
myService.getDataFeed(myMapPopularityFeedUri, handleDataFeed, handleError);
}
Help will be appreciated a lot. Please don't be too critical. Its my first question here. Instead let me know and I will make for my amendments.
Here is a screenshot of what the embed looks like upon my own click, i.e, after GA has
fetched my own data through my account. The geoMap is at the bottom of the page.
It seems the best way is creating of the separate shared (public) feed (report) in Analytics.
Just feed security requires authorization login to view or receive data if the feed (report) was created under personal credentials ... independently to what version OAuth or OAuth2 is used, just if you use request to view data on the site you should use at least API key (for public reports) OR OAuth credentials for personal reports.
look this guide:
Loading an API and Making a Request
or choose any other example from this page:
Google Analytics: Core Reporting API Client Libraries & Sample Code (v3)
Use this nice tool also:
OAuth 2.0 Playground
Generate API key for maps:
Google Maps API Family
Google Maps JS API tutorial:
Google Maps JavaScript API V3
Have you looked at using oAuth2 for the Authorization?

Categories

Resources