首页 > 解决方案 > 更改文件权限、超链接和通知:如何强制更新

问题描述

我有一个共享电子表格,其中包含指向保存在云端硬盘中的图像的超链接。传递链接时,用户会看到他们有权限的图像,而没有权限的人看不到它们。

当我更改权限(授予权限或删除权限)时,问题就出现了。在 3 到 5 分钟内,除非用户刷新整个页面或尝试打开图像,否则电子表格中的更改不会被欣赏,这会导致它们立即更新。

有没有办法通过应用程序脚本强制进行此更新?在工作表上编辑某些内容、由另一个用户更新整个页面或打开其他图像都没有效果。

一种“肮脏”的方法是创建图像的副本(将具有另一个 Id)并将超链接的 url 替换为具有新权限的副本的 url,但这需要一些执行时间。

我附上了通过 DriveApp 和 Drive 添加和删除权限的代码(我不知道为什么使用 Drive 删除有错误)

它还包括“脏”代码:如何获取超链接 url、获取文件和文件夹 ID、创建复制和删除原始内容、添加新权限以及将新 url 分配给超链接。也许有人可以为我指出一种更有效的方法(如果无法更新权限)。

谢谢!

function addPermission(fileId){  // to add permission to read file (image file in my case)
  var file = DriveApp.getFileById(fileId);
  var usu = 'user@gmail.com';
  file.addViewer(usu);
};

function delPermission(fileId){  // to remove permission
  var file = DriveApp.getFileById(fileId);
  var usu = 'user@gmail.com';
  file.removeViewer(usu);
};


function addPermissionWithoutMail(fileId) {  // to add permission to read file WITHOUT notification
  //value = email; type: "user", "group", "domain" or "default"; role: "owner", "writer" or "reader".
  var usu = 'user@gmail.com';
  var request = Drive.Permissions.insert({  // in 'Services' (on the left, under Libraries), add 'Drive API'
    'value': usu,
    'type': 'user',
    'role': 'reader'
  },
  fileId,
  {
    'sendNotificationEmails': false
  });
};

function delPermissionDrive(fileId){  // this function Not work. WHY??
  var permissionId2 = 'user@gmail.com'; //using the same email than addPermissionWithoutMail
  Drive.Permissions.remove(fileId,permissionId2); // ERROR: Permission not found: user@gmail.com
};



function changeImageLink(){          //  LAUNCH to change image associated with the hyperlink  **********
  
  var link = getUrlFromA1Link(); // get the url from hyperlink in a cell
  var Ids = getIDsFileFolder(link); // get Ids of file and folder of the link
  var file = DriveApp.getFileById(Ids[0]); // create a variable of file 
  var folder = DriveApp.getFolderById(Ids[1]); // create a variable of folder
  var newFileId = copyAndDelete(file,folder); // make a copy (with different Id) and delete original file 
  addPermissionWithoutMail(newFileId);// to add permission to read file (image file in my case)
  newLink(newFileId); // change url in hyperlink

};

function getUrlFromA1Link(){ // get the url from hyperlink in range A1
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange('A1');
  var hype = range.getRichTextValue();
  var link = hype.getLinkUrl();

  return link;
};

function getIDsFileFolder(link){ // get Ids of file and folder of the link
  var IdFileArray = link.match(/[-\w]{25,}/); //extract Id file
  var IdFile = IdFileArray[0];
  var file = DriveApp.getFileById(IdFile);
  var fileParents = file.getParents();
  while ( fileParents.hasNext() ) {
    var folder = fileParents.next();
    var IdFolder = folder.getId();
    //var nameFolder = folder.getName(); // to test is is correct folder
    break;  //only one parent is needed
  }
  var filfol = [IdFile, IdFolder];
  return filfol;    
};

function copyAndDelete(file,folder){ // make a copy (with different Id) and delete original file 
var newfile=file.makeCopy(file.getName()); //new file: the same name but different ID
folder.addFile(newfile);
folder.removeFile(file);
var IdFileNew = newfile.getId();
return IdFileNew;
};


function newLink(fileId){  // change url in hyperlink
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange('A1');
var link ='http://drive.google.com/uc?id=' + fileId;
var richText = SpreadsheetApp.newRichTextValue()
    .setText('Link for Image')
    .setLinkUrl(link)
    .build();
range.setRichTextValue(richText );
};

标签: google-apps-scriptpermissions

解决方案


你总会有一些传播延迟。一种更有效的方法是创建一个 Web 应用程序,并向用户显示个性化内容。


推荐阅读