我基本上想象的是可以改变允许编辑的工作表共享状态的命令。例如,我有一个公司的主文件(用于跟踪员工工作地点的“名册”以及他们在该区域工作的其他员工)以及员工用来跟踪进度的其他“区域表” 。人们每季度移动一次,所以我希望能够更新我的名册,然后让编程更新他们的工作表的共享状态,而不是点击每个工作表和复制粘贴的电子邮件。
据我所知,目前没有可能的方法来使用谷歌产品来实现这一目标。除了Google开发的新功能外,还有其他方法吗?
答案 0 :(得分:-1)
我不认为我完全理解你的要求,所以我将开始重述我所理解的内容。
对于第1点,您已经在Google表格中构建了列表,因此您可以将内容读入数组进行处理。您可以使用Google Apps Script Spreadsheet Service。示例:
var roster = SpreadsheetApp.getActive(); //assuming the script is running within the Roster spreadsheet
var employeesSheet = roster.getSheetByName("Employees"); //assuming sheet name is "Employees"
var employeeEmails = employeesSheet.getRange("B2:B").getValues(); //assuming the emails are in column B with the first row as header
//In this next line I'm going to make a very big simplification.
//I will assume that the coworkers emails are listed as "coworker1@company.com;coworker2@company.com" in the cell.
//This is likely not the case, but you may already have this formatted in a way that works.
//If not, you can merge these values in Google Apps Script, but I'll consider that outside the scope of this question.
var coworkerEmails = employeesSheet.getRange("C2:C").getValues();
对于第2点,我假设您已经有办法确定哪些文件需要修改其访问权限,并且您有权进行这些修改。您需要为此部分使用Drive Service。在我的示例中,我将假设所有相关文件都保存在特定于每个员工的文件夹中,并且文件夹ID保存在D列中。
var folderIds = employeesSheet.getRange("D2:D").getValues();
for (var i=0; i<employeeEmails.length; i++) {
var folder = DriveApp.getFolderById(folderIds[i][0]);
//At this point, you could give direct access to the entire folder,
//or just to the files within the folder, as I will do.
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
file.addEditors(coworkerEmails[i][0].split(";"));
}
}
请注意以上内容:
folder.getFiles()
命令 获取文件夹子项的文件。因此,如果文件夹有子文件夹,您还需要遍历这些文件夹。 最后,包括自定义菜单,您的代码可能会像:
function onOpen() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var menuEntries = [];
menuEntries.push({name: "Reset Permissions", functionName: "resetPermissions"});
ss.addMenu("Roster Actions", menuEntries);
}
function resetPermissions() {
var roster = SpreadsheetApp.getActive();
var employeesSheet = roster.getSheetByName("Employees");
var employeeEmails = employeesSheet.getRange("B2:B").getValues();
var coworkerEmails = employeesSheet.getRange("C2:C").getValues();
var folderIds = employeesSheet.getRange("D2:D").getValues();
for (var i=0; i<employeeEmails.length; i++) {
var folder = DriveApp.getFolderById(folderIds[i][0]);
var files = folder.getFiles();
while (files.hasNext()) {
var file = files.next();
file.addEditors(coworkerEmails[i][0].split(";"));
}
}
}