DriveAPITips

Transferring Owner of File to Other User using Google Apps Script

Gists

This is a sample script for transferring the ownership of a file to another user using Google Apps Script.

In the current stage, about the consumer account (gmail.com) the specification for transferring the ownership of a file has been changed as follows. Ref

  1. The current owner initiates an ownership transfer by creating or updating the prospective new owner’s file permission. The permission must include these settings: role=writer, type=user, and pendingOwner=true. If the new owner is creating a permission for the prospective owner, an email notification is sent to the prospective new owner indicating that they’re being asked to assume ownership of the file.
  1. The new owner accepts the ownership transfer request by creating or updating their file permission. The permission must include these settings: role=owner and transferOwnership=true. If the new owner is creating a new permission, an email notification is sent to the previous owner indicating that ownership has been transferred.

When this flow is reflected in the sample script, it becomes as follows.

Workaround: createdDate cannot be used with searchFiles of DriveApp in Google Apps Script

Gists

Unfortunately, in the current stage, in order to retrieve the file list using the created date, the search query of createdDate cannot be used with searchFiles method of DriveApp in Google Apps Script. This has already been reported at this issue tracker In this post, I would like to introduce a workaround for searching the files using the created date.

Issue and workaround

  • The parameter of “searchFiles” method of DriveApp uses the search query for Drive API v2. When I tested createdDate > '####-##-##' for “searchFiles” and “Files: list” of Drive API v2, I confirmed errors like Invalid argument: q and Invalid query occurred, respectively.

GAS Library - GASProjectApp

Overview

This is a Google Apps Script library for creating, updating and exporting Google Apps Script project of the standalone type using Drive API. In this case, Apps Script API is not used.

Description

I had reported “Drive API cannot create Google Apps Script project no longer” before. Ref About this, I had reported the future request. Ref At July 30, 2020, I could confirm that the Google Apps Script project of the standalone type got to be able to be created by multipart/form-data using Drive API again. Ref This is a good news for me. By this, in order to use this with Google Apps Script, I created this library. Because in this case, when the update method is used, the special scope of https://www.googleapis.com/auth/drive.scripts is required. So I thought that when this is published as the Google Apps Script library, this will be useful for users.

Drive API got to be able to create Google Apps Script project again

Gists

I have reported “Drive API cannot create Google Apps Script project no longer”. Ref About this, I had reported the future request. Ref Today, I could confirm that the Google Apps Script project of the standalone type got to be able to be created by multipart/form-data using Drive API. This is a good news for me. By this, the following 2 patterns can be used from now.

Pattern 1:

  1. Create new standalone GAS project by Apps Script API.
  2. Put the local script to the created GAS project by updating the project with Apps Script API.
  3. Move the GAS project from the root folder to the specific folder using Drive API.

In this pattern, 3 API calls are required.

Uploading Files of multipart/form-data to Google Drive using Drive API with Node.js

Gists

These are the sample scripts for uploading files of multipart/form-data to Google Drive using Drive API with Node.js. In this case, googleapis for Node.js is not used.

In these sample script, the maximum file size is 5 MB. Please be careful this. When you want to upload the files more than 5 MB, please check this report.

Sample script 1

This sample script uploads a file using the modules of fs and request. Before you use this script, please prepare your access token for uploading the file.

GAS Library - OwnershipTransfer

Overview

This is a Google Apps Script library for achieving the ownership-transfer of the specific folder including the files and sub-folders using Drive API.

IMPORTANT: PLEASE BE CAREFUL THIS.

At first, please read this section

I cannot take responsibility for the problems occurred by this library. So when you use this library, please use it by according to your own decision and at your own responsibility.

This GAS library transfers the ownership of files and folders. For example, when the ownership of a file is transferred to other account, the file cannot be managed by the original user. And also, when the ownership of the specific folder is transferred to other account, all files ans sub-folders in the specific folder cannot be managed by the original user. PLEASE BE CAREFUL THIS. So when you use this library, at first, please test this library using a sample file and folder. PLEASE BE CAREFUL THIS.

And also, the specification of ownership transfer might be updated by Google side in the future update. Because when I had tested this library, I could notice that the specification had been changed. Ref By this, I noticed that the transferred files had no parent folders, and also, the script was required to be modified. From this situation, when you use this script for your important files and folders, please be careful this. At first, PLEASE TEST THIS LIBRARY USING A SIMPLE FILE AND FOLDER.

Description

Recently, I had the situation that it is required to transfer the ownership of a folder including the files and sub-folders. When I manually transferred the ownership of the top folder from my account to other user’s account, although the ownership of top folder could be transferred, my account is left as the writer. And also, the ownership of the files and sub-folders in the top folder was not changed, while the user is the writer. Although I do the same situation using the method of create.permissions in Drive API, the same result was obtained. In the current stage, unfortunately, by the simple method, the complete ownership-transfer of the file including the files and sub-folders cannot be achieved. From this situation, I created the script for achieving the complete ownership-transfer of the files and folders as a Google Apps Script library.

Specification of Files: copy in Drive API was changed

I noticed that the specification of Files: copy in Drive API was changed. About the method of Files: copy in Drive API v3, this method could be used with the scope of https://www.googleapis.com/auth/drive.readonly until July 5, 2020. But I confirmed that in the current stage, the scope is required to be changed to the following scopes.

  • https://www.googleapis.com/auth/drive
  • https://www.googleapis.com/auth/drive.file
  • https://www.googleapis.com/auth/drive.appdata
  • https://www.googleapis.com/auth/drive.photos.readonly

Please be careful this.

Creating Shortcut on Google Drive using Google Apps Script

Gists

This is a sample script for creating a shortcut on Google Drive using Google Apps Script.

Sample script

Before you run the script, please enable Drive API at Advanced Google services.

function createShortcut(targetId, name, folderId) {
  const resource = {
    shortcutDetails: { targetId: targetId },
    title: name,
    mimeType: "application/vnd.google-apps.shortcut",
  };
  if (folderId) resource.parents = [{ id: folderId }];
  const shortcut = Drive.Files.insert(resource);
  return shortcut.id;
}

// Please run this function.
function main() {
  const targetId = "###"; // Please set the ID of target file or folder.
  const shortcutName = "###"; // Please set the shortcut name.
  const folderId = "###"; // Please set the folder ID for putting the created shortcut.
  const id = createShortcut(targetId, shortcutName, folderId);
  console.log(id);
}

References

GAS Library - CopyFolder

Overview

This is Google Apps Script library for copying folder on Google Drive.

Description

I have sometimes the situation that it is required to back up the folder on Google Drive to Google Drive. But unfortunately, the method of makeCopy() of Class File and the method of Files: copy of Drive API cannot be used for directly copying the folder on Google Drive. So I created this as a library. This library can copy the folder on Google Drive. All files in the folder can be copied to Google Drive with keeping the folder structure. When there are the files with newer modified time in the source folder than those in the destination folder, the files in the destination folder are overwritten by the newer files in the source folder. Also, in this library, the folders in the shared Drive and the publicly shared folders can be used as the source and destination folder.

Updating a File with Resumable Upload using Drive API

Gists

This is a sample flow for updating a file with the resumable upload using Drive API.

Sample situation:

In this answer, as a sample situation, it supposes that a text file in Google Drive is updated by the resumable upload with the multiple chunks. And as the method for requesting, I use the curl command.

I prepared 2 files for 2 chunks. As the test situation, the 2 chunks of 262,144 bytes and 37,856 bytes are uploaded. So total upload size is 300,000 bytes. Those filenames are data1.txt and data2.txt, respectively.

Retrieving Files and Folders without Parents in Google Drive

Gists

This is a sample script for retrieving the files and folders which have no parents in own Google Drive.

When you use this script, please enable Drive API at Advanced Google services.

Sample script

const myFunction = () => {
  const token = ScriptApp.getOAuthToken();
  const fields = decodeURIComponent(
    "nextPageToken,files(name,id,mimeType,parents)"
  );
  const q = decodeURIComponent("'me' in owners and trashed = false");
  let files = [];
  let pageToken = "";
  do {
    const res = UrlFetchApp.fetch(
      `https://www.googleapis.com/drive/v3/files?pageSize=1000&fields=${fields}&q=${q}&pageToken=${pageToken}`,
      { headers: { authorization: `Bearer ${token}` } }
    );
    const obj = JSON.parse(res);
    Array.prototype.push.apply(files, obj.files);
    pageToken = obj.nextPageToken;
  } while (pageToken);
  const result = files.filter(({ parents }) => !parents);
  console.log(result);
};

When you run the script, the files and folders which have no parents in own Google Drive are retrieved.

Drive API cannot create Google Apps Script project no longer

Gists

Today, I noticed that new Google Apps Script project of the standalone script type cannot be created by the method of Files: create in Drive API. From now, in order to manage the Google Apps Script project, only Google Apps Script API is required to be used. By this, the following issues are brought.

  • When the new standalone GAS project is created in the specific folder by uploading the local script, the following flow is required to be run.
    1. Create new standalone GAS project by Apps Script API.
    2. Put the local script to the created GAS project by updating the project with Apps Script API.
    3. Move the GAS project from the root folder to the specific folder using Drive API.

From now, 3 API calls are required to be used like above. By the way, this had been able to be achieved by one API call of the method of files.create in Drive API before.

Moving File to Specific Folder using Google Apps Script

Gists

These are 3 sample scripts for moving a file to the specific folder in Google Drive using Google Apps Script.

Sample script 1

In this script, only Drive Service is used.

var sourceFileId = "###";
var destinationFolderId = "###";

var file = DriveApp.getFileById(sourceFileId);
DriveApp.getFolderById(destinationFolderId).addFile(file);
file
  .getParents()
  .next()
  .removeFile(file);

Sample script 2

In this script, only Drive API at Advanced Google services. (In this case, it’s Drive API v2.)

var sourceFileId = "###";
var destinationFolderId = "###";

Drive.Files.update({ parents: [{ id: destinationFolderId }] }, sourceFileId);

Sample script 3

In this script, only Drive API v3 is used.

One Time Download for Google Drive

Overview

This is a sample script for downloading files from Google Drive by the one time download method.

Description

When you download a file from Google Drive, in generally, the login and the access token are required. If you want to download the file without the authorization for the simple situation, the file is required to be publicly shared. But the file might not be able to be shared publicly, because of various reasons.

Uploading File to Shared Folder using ggsrun

Gists

ggsrun is also a CLI application for using Google Drive.

Here, I would like to introduce a sample command. This is a sample command for uploading a file to a shared folder using ggsrun.

This situation supposes that the shared folder is https://drive.google.com/drive/folders/abcdefg?usp=sharing and the folder has the edit permission.

Sample command:

$ ggsrun u -f "sample.txt" -p "abcdefg" --serviceaccount "###JSON file of Service Account###"
  • If you have already used OAuth2, you can upload the file by ggsrun u -f "sample.txt" -p "###folderId###".

(NEW) Retrieve old revision file from Google Drive

This method was updated at July 12, 2017.

In order to use this, at first, please retrieve your access token and enable Drive API.

1. File ID

Retrieve file id from file name.

curl -X GET -sSL \
    -H 'Authorization: Bearer ### Access token ###' \
    'https://www.googleapis.com/drive/v3/files?q=name="### FileName ###"&fields=files(id,name)'

Reference : https://developers.google.com/drive/v3/reference/files/list

2. Revision ID

Retrieve revision id from file id.

curl -X GET -sSL \
    -H 'Authorization: Bearer ### Access token ###' \
    'https://www.googleapis.com/drive/v3/files/### FileID ###/revisions?fields=revisions(id%2CmodifiedTime)'

Reference : https://developers.google.com/drive/v3/reference/revisions/list

Retrieve old revision file from Google Drive

I introduce 2 kinds of methods. One is to use curl. Another is to use wget. At this time, I could know that wget can be also used as same as curl.

In order to use this, at first, please retrieve your access token and enable Drive API.

1. File ID

Retrieve file id from file name.

curl -X GET -sSL \
    -H 'Authorization: Bearer ### Access token ###' \
    'https://www.googleapis.com/drive/v3/files?q=name="### FileName ###"&fields=files(id,name)'
wget -q --header='Authorization: Bearer ### Access token ###' \
    'https://www.googleapis.com/drive/v3/files?q=name="### FileName ###"&fields=files(id,name)' \

Reference : https://developers.google.com/drive/v3/reference/files/list