Skip to content

Commit 7cb7563

Browse files
committed
Read-only Google Drive
1 parent 884f3c0 commit 7cb7563

File tree

3 files changed

+35
-0
lines changed

3 files changed

+35
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Google Drive Lock 🐢
2+
3+
[Make files Read-only in Google Drive](https://www.labnol.org/code/read-only-google-drive-file-201011)
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"timeZone": "Asia/Kolkata",
3+
"dependencies": {},
4+
"runtimeVersion": "V8",
5+
"oauthScopes": ["https://www.googleapis.com/auth/drive", "https://www.googleapis.com/auth/script.external_request"]
6+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
/**
2+
* Make Google Drive files Read only
3+
* Author: amit@labnol.org
4+
* Web: https://digitalinspiration.com/
5+
* MIT License
6+
**/
7+
8+
const makeFileReadyOnly = () => {
9+
const fileUrl = "https://docs.google.com/document/d/.....";
10+
const [fileId] = fileUrl.split("/").filter((e) => /[_-\w]{25,}/.test(e));
11+
UrlFetchApp.fetch(`https://www.googleapis.com/drive/v3/files/${fileId}`, {
12+
method: "PATCH",
13+
contentType: "application/json",
14+
headers: {
15+
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`,
16+
},
17+
payload: JSON.stringify({
18+
contentRestrictions: [
19+
{
20+
readOnly: true,
21+
reason: "Prevent accidental editing",
22+
},
23+
],
24+
}),
25+
});
26+
};

0 commit comments

Comments
 (0)