[英]How to allow users to edit protected range through script?
I have a sheet with a protected range which permissions are set to only allow me to edit it. 我有一个带有受保护范围的工作表,其权限设置为仅允许我对其进行编辑。 I have a function in the sheet's script which edits cells in the protected range.
我在工作表的脚本中有一个功能,可以编辑受保护范围内的单元格。 I rigged up the function to fire on edit which allows users other than myself to run the function successfully since it runs using my permissions.
我装配了该功能以在编辑时触发,这允许除我自己以外的其他用户成功运行该功能,因为该功能使用我的权限运行。
So that scenario is all fine and dandy, however I complicate things by wanting to display a loading message while the function is running. 因此,这种情况很好,但我想通过在函数运行时显示加载消息来使事情复杂化。 To do this I created the html file below:
为此,我创建了以下html文件:
<script>
function onSuccess() {
google.script.host.close();
}
function onFailure() {
var div = document.getElementById('result');
div.innerHTML = '<div>Data failed to load.</div>';
}
google.script.run.withSuccessHandler(onSuccess).withFailureHandler(onFailure).process();
</script>
<div id='result'><img src="https://c4a54d10381f750e81dcc323aed21e2c95725815.googledrive.com/host/0Bwyqwd2fAHMMallsNkNOV0RfcTg/wait_progress.gif"></div>
The html is displayed when the following function is triggered by the on edit event: 当on edit事件触发以下功能时,将显示html:
function refresh() {
var html = HtmlService.createHtmlOutputFromFile('loading').setSandboxMode(HtmlService.SandboxMode.IFRAME).setWidth(100).setHeight(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Processing...');
}
The user receives an error saying that he/she cannot edit a protected cell. 用户收到一条错误消息,指出他/她无法编辑受保护的单元格。 I assume this happens because the refresh() function runs with my permissions but the process() function runs with his/her permissions.
我认为发生这种情况是因为refresh()函数使用我的权限运行,而process()函数使用他/她的权限运行。 Any ideas on how I can make this work for me?
关于如何使这项工作对我有任何想法?
Edit - I tried to use the following code to solve my problem: 编辑-我试图使用以下代码来解决我的问题:
function refresh() {
setPermission(1);
SpreadsheetApp.flush();
var html = HtmlService.createHtmlOutputFromFile('loading').setSandboxMode(HtmlService.SandboxMode.IFRAME).setWidth(100).setHeight(100);
SpreadsheetApp.getUi().showModalDialog(html, 'Processing...');
SpreadsheetApp.flush();
setPermission(0);
}
function setPermission(flag)
{
var me = Session.getActiveUser();
var ss = SpreadsheetApp.getActive();
var protections = ss.getProtections(SpreadsheetApp.ProtectionType.RANGE);
var protection = protections[0];
if (flag == 1)
{
protection.addEditor(me);
} else {
protection.removeEditor(me);
}
}
It starts to work because the process() function does change some cells in the protected range but then the same error saying the user cannot edit the protected range shows up. 它开始工作是因为process()函数确实更改了受保护范围内的某些单元格,但随后出现了相同的错误,提示用户无法编辑受保护范围。 I believe it's because showModalDialog doesn't suspend the script so before process() can finish the user's permission to edit the protected range is taken away.
我相信这是因为showModalDialog不会暂停脚本,因此在process()可以完成用户对受保护范围进行编辑的权限之前,就将其删除。
There are a couple ways to do this. 有两种方法可以做到这一点。
1) if you know how to do the oauth calls manually (an oauth2 apps script library is out there to help) you could have your account refresh token saved and use it to make the sheet api calls in your name (through urlFetch). 1)如果您知道如何手动执行oauth调用(有一个oauth2应用程序脚本库可以提供帮助),则可以保存您的帐户刷新令牌,并使用该令牌以您的名字(通过urlFetch)进行工作表api调用。
2) publish another script that runs in your name with anonymous which does the changes by receiving url parameters. 2)发布另一个以您的名字运行且匿名的脚本,该脚本通过接收url参数进行更改。 Call that script from your original script with urlFetch.
使用urlFetch从原始脚本中调用该脚本。 this is easier to code that (1) but might me slower.
这更容易编写(1),但可能会使我变慢。
you might also need to use a custom installed onEditTrigger if you have permission issues with urlFetch or other apis. 如果您对urlFetch或其他api有权限问题,则可能还需要使用在onEditTrigger上安装的自定义项。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.