LoginSignup
3
3

More than 3 years have passed since last update.

[GAS] 相手へ通知なしでファイル閲覧権限を付与する方法

Last updated at Posted at 2021-03-20

結論

GAS標準のDriveAppでなく、GoogleDrive APIを使用する。

DriveAppを使用する場合 (通知設定不可)

僕みたいに基本ググってコピペでGASを書いて(?)いる方は、通常DriveAppを使って、ファイル(やフォルダ)に対して権限付与を行うと思います。

以下の感じです。


function addViewer_driveApp(){
  const targetFile = DriveApp.getFileById('******');
  targetFile.addViewer('***@gmail.com');
}

これだと、権限付与時の通知設定はできず、必ず相手に通知が飛んでしまいます。

GoogleDrive APIを使用する場合 (通知設定可)

権限付与時の通知ON/OFF設定をするには、GoogleDrive APIを使用する必要があります。簡単です。

事前準備

スクリプトのプロジェクトを開いて、「Service」 - 「Drive API」をONにする。

スクリプト


function addViewer_googleDriveApi(){
  // 
  const targetFile = DriveApp.getFileById('******');
  const fileId = targetFile.getId();
  const email = '***@gmail.com';
  Drive.Permissions.insert(
   {
     'role': 'reader', //権限タイプを選ぶ (owner, organizer, fileOrganizer, writer, reader)
     'type': 'user', //アカウントタイプを選ぶ (user, group, domain, anyone)
     'value': email
   },
   fileId,
   {
     'sendNotificationEmails': 'false' //true=通知ON, false=通知OFF
   });
}

以上です

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3