You can lock your files, but unfortunately that is not a very simple action. No one can edit the file after locking, but unlocking is relatively straight forward. Here are the steps you need to take (Credit to https://www.labnol.org/code/read-only-google-drive-file-201011)
- Get the shareable link for the file you want to lock. Right-click on the file, click on the "get link" option and copy the link that you will see in the pop-up menu. Set the permissions of the link to edit, otherwise, the script can't lock the file.
- Create a new tab in your browser and go to the URL
https://script.new
. This will open a new Google App script.
- Copy the following code in the editor:
const makeFileReadyOnly = () => {
const fileUrl = '<<FILE URL>>';
const [fileId] = fileUrl.split('/').filter((e) => /[_-\w]{25,}/.test(e));
UrlFetchApp.fetch(`https://www.googleapis.com/drive/v3/files/${fileId}?supportsAllDrives=true`, {
method: 'PATCH',
contentType: 'application/json',
headers: {
Authorization: `Bearer ${ScriptApp.getOAuthToken()}`,
},
payload: JSON.stringify({
contentRestrictions: [
{
readOnly: true,
reason: 'Prevent accidental editing',
},
],
}),
});
// For requesting correct scope, do not delete
// var file = DriveApp.getFileById().setName()
};
- Replace the
<<FILE URL>>
in line #2 with the URL of the Drive file that you copied in the previous step.
- Save project and run the function. You will be asked for permissions, accept them and the file will be locked.
- If you wish to unlock the file, simply right click on the file and select "unlock". You can find the script in https://script.google.com/home if you want to lock the file again.