Automatic Syncing between two Google Drive Shared Drives - google-apps-script

Question:
I have two Google Shared Drives (Team Drives), let's say Movies and MoviesBackup. I need to backup whatever I upload to Movies into MoviesBackup. For this, I need an unidirectional sync from Movies to MoviesBackup, once daily.
What I have tried:
I know of of Rclone, and have used its Command Line Interface to sync between two Shared Drives. Maybe if Rclone can be used from Google AppScript, I would set a daily trigger. But I seem to find no way to do so.
Any other solutions that work will be appreciated.

Although I'm not sure whether this is the direct solution of your issue, in your situation, how about the following sample script? This sample scripts uses a Google Apps Script library. Ref
When this library is used, a source folder can be copied to the specific destination folder. And, when the files in the source folder are updated, the updated files are copied to the destination folder as the overwrite.
Usage:
1. Install library.
Please install the library to your Google Apps Script project. The method of installing it can be seen at here.
2. Sample script.
Please copy and paste the following script to the script editor of your Google Apps Script project. And save it. And, in this library, Drive API is used. So please enable Drive API at Advanced Google services.
And, please set the source and destination folder IDs to the following object.
function myFunction() {
const object = {
sourceFolderId: "###", // Please set the source folder ID.
destinationFolderId: "###", // Please set the destination folder ID.
overwrite: true,
};
const res = CopyFolder.copyAllFilesFolders(object);
console.log(res);
}
Note:
When this script is run for the 1st time, all files are copied. And, after the script is run as 2 times, only the updated files are copied.
Reference:
CopyFolder of Google Apps Script library

Related

Share the Drive file with extra permissions

I have a drive file where I need to restrict the users from these (I mean ticks for both needs to be removed and shared using apps script/drive api)
I need to accomplish using Google Apps Script Advanced Drive service or regular apps script. I am unsure what is the exact method. I have tried with
function shareit() {
DriveApp.getFileById(id).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT);
}
Drive Sharing Permissions with App Script
I manage to make both changes by using Advance Services from App Script. I was able to use part of your code to first remove the "Editors can change permissions and share" by using the:
setShareableByEditors(false)
Code Sample:
function shareit() {
DriveApp.getFileById(id).setSharing(DriveApp.Access.ANYONE_WITH_LINK, DriveApp.Permission.EDIT).setShareableByEditors(false);
// Add advance service to be able to run a files patch and update the permissions of the ID to require writters permisions to copy, print and download.
Drive.Files.patch({copyRequiresWriterPermission:true}, id);
}
As presented in the comments you would need run a patch update to the File ID to change the permissions.
References:
https://developers.google.com/apps-script/advanced/drive
https://developers.google.com/drive/api/v2/reference/files/patch
https://developers.google.com/apps-script/reference/drive/file#setShareableByEditors(Boolean)

Creating a Google App Script web app to upload files to google drive with minimum scope

I want to create a Google App Script web app to allow users to upload files to a folder of my google drive.
The problem is that I've checked the drive scope list here,
in order to perform the upload action , this scope must be reviewed by the users
https://www.googleapis.com/auth/drive
and it comes with a scary description See, edit, create and delete all of your Google Drive files which is way too strong.
Is there any way to perform the upload action with a less stronger scope?
Thank you so much.
In your situation, for example, how about the following scope?
https://www.googleapis.com/auth/drive.file
The official document says as follows. Ref
Per-file access to files created or opened by the app. File authorization is granted on a per-user basis and is revoked when the user deauthorizes the app.
In this scope, only the files and folders created by this application can be accessed.
Reference:
Authenticate your users
Added:
From your following replying,
Thank you for replying too, I have an update. When I run the code in the editor , it throws an error : ' { [Exception: You do not have permission to call DriveApp.Folder.createFile. Required permissions: googleapis.com/auth/drive] name: 'Exception' }' So I guess there is no solution to my problem T.T
I understood that you are using createFile with the Drive service (DriveApp). In this case, the scope of https://www.googleapis.com/auth/drive is requierd to be used. It seems that this is the current specification. In order to use the scope of https://www.googleapis.com/auth/drive.file, how about using Drive API at Advanced Google services? By this, you can use the scope by setting to appsscript.json which is the manifest file.
At first, please add https://www.googleapis.com/auth/drive.file as "oauthScopes": ["https://www.googleapis.com/auth/drive.file"] to the manifest file of appsscript.json. Ref
Although I'm not sure about your actual script, the sample script for creating a Spreadsheet is as follows.
Sample script:
Before you use this script, please enable Drive API at Advanced Google services.
function myFunction() {
const obj = Drive.Files.insert({title: "sampleSpreadsheet", mimeType: MimeType.GOOGLE_SHEETS, parents: [{id: "###folderId###"}]});
console.log(obj.id)
}
When you run this script, new Spreadsheet is created to folderId by the scope of https://www.googleapis.com/auth/drive.file.
Note:
If you are required to use other scopes, please add them to oauthScopes.
References:
Manifests
Manifest structure
Related thread.
Can I use DocumentApp.openById() with read only permission?

Use a google sheet to show file updates on google drive

Maybe this is already known, or there is a better way if you know please tell me.
I want a google spreadsheet that shows me the last updated file on each folder on googledrive. and that it upgrades itself if new folders or files are added.
For example, i have a main Folder, the spreadsheet should tell me about updates subfolders A B C D
Folder name, lastfileupdatedname in that folder , Who updated it and when.
Since asked specifically about apps-script; two options come to mind:
Advanced Google Services\Google Drive Activity Service [ Google Drive Activity API in Apps Script]
https://developers.google.com/apps-script/advanced/drive-activity
or
use a folder/file iterator
https://developers.google.com/apps-script/reference/drive/folder-iterator

Search string in all scripts (including Container-bound files) in GAS

I am trying to search for a string in all my files (scripts and container-bound files, like scripts in spreadsheets or docs) and I am not having luck with that.
The following code successfully retrieves the scripts that have the text 'var':
function myFunction() {
var fs = DriveApp.getRootFolder().searchFiles('fullText contains "var"');
while (fs.hasNext()) {
var file = fs.next();
Logger.log(file.getName());
}
}
But it does not retrieve the scripts in container-bound files. Even if I try inside container-bound files, it still lists all standalone scripts with the text, but not container-bound ones (not even the one I am executing it from).
Any clue? Thanks!!
Consideration
Container-bound Script Projects are not indexed by the Google Drive API. It's not possible to retrieve the Project file using the FileID/ProjectID.
This feature has been requested
This feature is currently unavailable, but it has been requested. Feel free to star the issue to increase its visibility.

How can I automate OAuth requests in Google Sheets when cloning file? [duplicate]

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