I am trying to copy the sheet from one account from another account while accessing google form. I am able to copy the sheet by using below Appscript. But i need to open the same while copying itself. Currently able open by manually.
function myFunction() {
var app=SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1DpeqCN2Bm2pWxf4TiMEa53xM6MEhSx8GfP6CuyI/edit#gid=0");
app.copy("NewSheettest19");
}
Opening the file in user context
You can't trigger a script on form submission that runs in the user context. Installed triggers run with the owner's privileges. This could result in security issues.
You may email the user with the link or insert it in a shared spreadsheet cell. But you cannot access the user interface in this context.
Please look at this answer for additional information: https://stackoverflow.com/a/26978896.
Opening the file in the editor context:
You won't be able to open the copied file directly from Apps Script. You can use though a workaround building a user interface that uses javascript to open a new page, navigating to your recently copied spreadsheet file.
Code:
var app=SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1DpeqCN2Bm2pWxf4TiMEa53xM6MEhSx8GfP6CuyI/edit#gid=0");
var copied = app.copy("NewSheettest19");
var html = "<script>window.open('" + copied.getUrl() + "');google.script.host.close();</script>";
var userInterface = HtmlService.createHtmlOutput(html);
FormApp.getUi().showModalDialog(userInterface, "Opening Copied Sheet... ");
This code will create a custom dialog containing the Javascript to open the new Spreadsheet and then it will close the dialog.
References:
G Suite Docs Dialogs
Window.open()
Related
I've set up a script that essentially copies the contents from a 'template' folder within my google drive and moves the copied contents to a newly created folder elsewhere. The 'Template' folder will always hold 9 individual spreadsheets, each with their own unique bounded script.
The problem I'm having is that every time I copy the spreadsheets over, I have to reauthorise access for each of the 9 scripts before I can start using the functionality I've created.
I'd like to be able to assign or grant permission for the bound script to access the services it needs to during the process I use to copy the spreadsheet to a new location.
Here is an example of the code I use to copy over the spreadsheets. Is there anyway to access the script to assign permissions here?
function copyContents(template_folder, new_folder){
var files = template_folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var file_name = file.getName()
var copied_file = file.makeCopy(file_name,new_folder)
// Assign permissions here...
}
}
It's not possible to grant authorization programmatically as this implies a security risk. The alternative is to publish your bounded scripts as add-ons so you authorize them once and you will be able to use them on any spreadsheet.
Bear in mind that you still should have to enable the addons on each of the documents you want to use them but this usually as simple as having a an onOpen menu that runs with ScriptApp.authMode.NONE
Note: I'm pretty sure that this was already asked and answered on this site.
Similar questions without answers with positive score
Installable onEdit trigger in google spreadsheet script when copying a template spreadsheet script
Google Script Carrying over triggers when copying a spreadsheet
I've set up a script that essentially copies the contents from a 'template' folder within my google drive and moves the copied contents to a newly created folder elsewhere. The 'Template' folder will always hold 9 individual spreadsheets, each with their own unique bounded script.
The problem I'm having is that every time I copy the spreadsheets over, I have to reauthorise access for each of the 9 scripts before I can start using the functionality I've created.
I'd like to be able to assign or grant permission for the bound script to access the services it needs to during the process I use to copy the spreadsheet to a new location.
Here is an example of the code I use to copy over the spreadsheets. Is there anyway to access the script to assign permissions here?
function copyContents(template_folder, new_folder){
var files = template_folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var file_name = file.getName()
var copied_file = file.makeCopy(file_name,new_folder)
// Assign permissions here...
}
}
It's not possible to grant authorization programmatically as this implies a security risk. The alternative is to publish your bounded scripts as add-ons so you authorize them once and you will be able to use them on any spreadsheet.
Bear in mind that you still should have to enable the addons on each of the documents you want to use them but this usually as simple as having a an onOpen menu that runs with ScriptApp.authMode.NONE
Note: I'm pretty sure that this was already asked and answered on this site.
Similar questions without answers with positive score
Installable onEdit trigger in google spreadsheet script when copying a template spreadsheet script
Google Script Carrying over triggers when copying a spreadsheet
I've set up a script that essentially copies the contents from a 'template' folder within my google drive and moves the copied contents to a newly created folder elsewhere. The 'Template' folder will always hold 9 individual spreadsheets, each with their own unique bounded script.
The problem I'm having is that every time I copy the spreadsheets over, I have to reauthorise access for each of the 9 scripts before I can start using the functionality I've created.
I'd like to be able to assign or grant permission for the bound script to access the services it needs to during the process I use to copy the spreadsheet to a new location.
Here is an example of the code I use to copy over the spreadsheets. Is there anyway to access the script to assign permissions here?
function copyContents(template_folder, new_folder){
var files = template_folder.getFiles();
while (files.hasNext()) {
var file = files.next();
var file_name = file.getName()
var copied_file = file.makeCopy(file_name,new_folder)
// Assign permissions here...
}
}
It's not possible to grant authorization programmatically as this implies a security risk. The alternative is to publish your bounded scripts as add-ons so you authorize them once and you will be able to use them on any spreadsheet.
Bear in mind that you still should have to enable the addons on each of the documents you want to use them but this usually as simple as having a an onOpen menu that runs with ScriptApp.authMode.NONE
Note: I'm pretty sure that this was already asked and answered on this site.
Similar questions without answers with positive score
Installable onEdit trigger in google spreadsheet script when copying a template spreadsheet script
Google Script Carrying over triggers when copying a spreadsheet
I am new to Google Sites, I have created a test google site, and add some pages files from google drive to the pages.
My Question is: Is it possible to set permissions from a Google Site while adding a file/folder from Google Drive via any Google Apps Script or Google Drive SDK?
I also did some overview about Google Apps Script for adding UI elements/ html elements, and perform some basic authentication using Google Drive SDK. Given this:
Is possible to perform permission actions.
If we upload a file in a file cabinet template can we set any kind of permissions or remove download link button of that file?
Apps Script has a service called Drive. Drive has a class named: DriveApp. DriveApp can create, save and retrieve files. Once you have a reference to a file, you can use other Classes, Properties and Methods.
setSharing method - Google Documentation
Thanks to Your Answer #Sandy Good from you suggestion/idea I able to create a simple script and after creating this script I am sure I will be able to create a complicated script also.
I have created a script that will create a form in in google site's page. this form contain a dropdown list of my google drive files and an input filed in which i will add an email address.
and in the last a submit button.
after pressing submit button , the script work as it will grant permission on a selected to the email given as viewer of the file if he has link.
create a blank script in the Google Site's Apps Script Section.
Add some UI via UI Service
function doGet() {
// create ui element
var app = UiApp.createApplication();
// create form
var form = app.createFormPanel();
// create area or panel
var flow = app.createFlowPanel();
// create list box for files
var element_file = app.createListBox().setId('file_name').setName('file_name');
// add first elemnt
element_file.addItem('Please Select','');
// get drive files
var files = DriveApp.getFiles();
while (files.hasNext()) {
var file = files.next();
element_file.addItem(file.getName(),file.getId());
}
flow.add(element_file);
var element_text=app.createTextBox().setId('email').setName('email');
flow.add(element_text);
flow.add(app.createSubmitButton("Submit"));
form.add(flow);
app.add(form);
return app;
}
after this I handle the data submitted via post
function doPost(e) {
var app = UiApp.getActiveApplication();
var email = e.parameter.email;
var file_name = e.parameter.file_name;
var public_file = DriveApp.getFileById(file_name);
public_file.addViewer(email);
return app;
}
after this you can check the permissions of the specific file in the google drive.
you may also add a success message.
after this go to publish menu of the file select deploy as web app then select the required things. remember please select execute scripts only as me option because you are listing all your files.
then go to the page in which you want to add script just edit/add new page from editor of the page just insert Apps Script.
apologies , its my first time to work in google apps script , if I miss anything or i did not follow any rule etc.
Thanks,
In a little over my head. Trying to use UrlFetchApp.fetch(url, options) from a container-bound script to run code in a web app.
The problem: Container-bound doc script can only run as the activeUser. doc script creates a new doc by copying a template. I would like the doc newly created from the template to be stored in a centralized folder owned by the developer. I see two solutions.
I give all domain users view/edit access to the developer's folder.
I create a web app from a standalone script which runs as the effectiveUser (developer) who has access to the folder. In this case the doc script calls the web app using UrlFetchApp passing in the parameters (folder, doc). However, to quite able to figure out how to do this, if possible.
var unitId = DocumentApp.getActiveDocument().getId();
var unit = DriveApp.getFileById(unitId);
var folderId = unit.getDescription() //FoldId for unit is stored in description
var folder = DriveApp.getFolderById(folderId);
var lesson = DriveApp.getFileById(UNIT.LESSON_TEMPLATE_ID).makeCopy('Lesson - ' + result.getResponseText());
folder.addFile(lesson);//Currently I have the folder shared/edit with domain users.
//I would prefer to share/view. However, since the
//container-bound doc script runs only as active user, no
//can do. Is it possible to build a small web app which
//runs as effective user and saves lesson to folder.
showLessonSidebar(folderId);
Any hints out there?
Yes, it is possible to create a web app that runs as effective user.If the developer - effective user has access to folder and template file.
You could pass the folderId and Name of the file to be copied to the WebApp, rather than passing folder and Doc object (passing the folder Object and Doc object as URL parameter may be quite tricky and if content is too large may not be even possible as there are constraints on the possible length of URL)
1) Write a Apps Script
function doGet(e) {
var folderId = e.parameters.folderId;
var copyFileName = e.parameters.name;
var userEmail = e.parameters.email;
var folder = DriveApp.getFolderById(folderId);
var lesson = DriveApp.getFileById(UNIT.LESSON_TEMPLATE_ID).makeCopy(copyFileName);
folder.addFile(lesson);
//share file to domain or share folder with domain - folder.setSharing(....);
file.setSharing(DriveApp.Access.DOMAIN, DriveApp.Permission.VIEW);
}
2) While deploying the Web App, developer can set Execute as me(developer#...) and set "Who has access to Web App" as Anyone.
3) Make sure developer has authorized the web App.
4) Share the Web App Script file with all your domain users.
Also, if you want to pass the content of file for some reason from container-script to Web App, you may consider implementing the logic in doPost() as you would have the ability to pass large data using POST in UrlFetch.
As far as I understand, the only way to use UrlFetch in a bound script, is using an installed trigger. (UrlFetch is not authorized to run when used in a bound script's simple triggers: https://developers.google.com/apps-script/add-ons/lifecycle#authorization_modes.)
An installed trigger always "runs with the authorization of the user who created the trigger, even if another user with edit access opens the spreadsheet". Thus, simply use installed triggers:
function setupTrigger(){
var doc = DocumentApp.getActiveDocument();
ScriptApp.newTrigger('open').forDocument(doc).onOpen().create();
}
This sample code creates an installable trigger for opening the document bound to the script.