问题描述
我有一个谷歌脚本,它从输入文件 (1) 中获取数据并将其复制到输出文件 (2)。
输出文件 (2) 进行所有计算,并将一张表格发送到特定的电子邮件,其中包含报价和最终价格的简历。
只要用户对这两个文件都拥有编辑权限,此脚本就可以正常工作,但我不想在输出文件 (2) 上授予编辑权限。
是否可以仅在脚本运行时将编辑权限授予某人?
这是我的代码:
function INput_OUTput() {
var INput_File = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1OGB4cIfFR8GKW2Ph7W7Tx_pgF7WsHitgKJZd0s8irco/edit");
var OUTput_File = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/1E7G3c4-uT1_fWKk36GnJO0ga-bMpvuTt2NH1jKvDUgU/edit");
const userEmail = "person@gmail.com";
OUTput_File.addEditor(userEmail);
var Input_File_sheet1 = INput_File.getSheetByName('sheet1');
var OUTput_File_sheet1 = OUTput_File.getSheetByName('sheet1');
var OUTput_File_calculationsheet = OUTput_File.getSheetByName('calculationsheet');
////// copy sheet////////////////////////
// Get full range of data
var SRange = Input_File_sheet1.getDatarange();
// get A1 notation identifying the range
var A1Range = SRange.getA1Notation();
// get the data values in range
var SData = SRange.getValues();
// Clear the Google Sheet before copy
OUTput_File_sheet1.clear({contentsOnly: true});
// set the target range to the values of the source data
OUTput_File_sheet1.getRange(A1Range).setValues(SData);
///////////hide calculation sheets
OUTput_File_sheet1.hideSheet();
OUTput_File_calculationsheet.hideSheet();
///////////send email
var message = {
to: userEmail,subject: "Calculation result",body: "Hello,\n\nThanks for your ....\n\n\The Team",name: "MyName",attachments: [OUTput_File.getAs(MimeType.PDF).setName("BUDGET result")]
}
MailApp.sendEmail(message);
OUTput_File.removeEditor(userEmail);
}
例外:您无权访问所请求的文档
解决方法
我已完成您的建议(删除评论并编辑我的问题),但我必须保留此答案以保留我们的评论。
,作为一种不同的方法,您可以部署网络应用
您提到您有 2 个文件:
- 输入文件:共享
- 输出文件:受限制
如果您不想共享输出文件,而是以您(编辑者)的身份执行 INput_OUTput()
函数。创建一个独立的 Google App 脚本并将函数名称重命名为 doGet(e)
或 doPost(e)
(取决于您要使用的 HTTP 方法),如 documentation 所述,然后将您之前的函数复制到新函数和 Deploy your App 但请记住您可以设置用户 who executes the Web App。
上面提到的是如何实现 Web 应用程序,以便在不直接共享文件的情况下管理 Google Apps 脚本中的数据,因为您可以指定运行应用程序的用户,而不管请求 PDF 的用户是谁。
>然后,如果您想调用 doGet 或 doPost,请使用 Class UrlFetchApp 直接调用 Web App。