我可以阻止具有编辑权限的人修改我的Google Apps脚本吗? [英] Can I prevent people with edit access from modifying my google apps script?

查看:279
本文介绍了我可以阻止具有编辑权限的人修改我的Google Apps脚本吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在特定的电子表格上拥有一个Google Apps脚本,并已被授予从我的帐户写电子邮件的权限.其他一些人也具有对电子表格的编辑"访问权限.我想知道是否有什么办法可以防止具有编辑"访问权限的人员修改我的脚本并从帐户发送大量电子邮件?

I have a google apps script on a particular spreadsheet to which I have given permission to write an email from my account. A few others have 'edit' access to the spreadsheet as well. I was wondering if there is any way for me to prevent the people with 'edit' access from modifying my script and sending rouge emails from account?

我探索了诸如以下的选项:

I explored options such as:

  1. 在个人工作表上创建一个库,并读取其他人正在访问的代码.这样只会对其他人隐藏代码,但是他们仍然可以写电子邮件,因为脚本区域已经具有写电子邮件的权限.
  2. 创建附加组件,这是我目前正在调查的内容.

在下面,您可以看到我的脚本示例,该示例是在电子表格中进行编辑时触发的.

Below you can see an example of my script which is triggered on edits inside the spreadsheet.

function onEditTrigger(e) 
{
  var row = e.range.rowStart;
  var column=e.range.columnStart;
  
  if (row==1 && column ==1)
  {
  GmailApp.sendEmail("xxx@gmail.com","Subject","Email content");
  }
  
  }

我想要做的是当任何人编辑A1单元时从我的帐户发送一封电子邮件.但是我想控制从我的邮箱发送的内容.目前,任何可以编辑工作表的人都可以编辑从邮箱发送的电子邮件,这是我不喜欢的.

What I want to do is to send an email from my account when anyone edits the A1 cell. But I want control over what is sent from my mailbox. Currently anyone who can edit the sheet can edit the email that is sent from mailbox as well, which is what I do not like.

我可以限制人员,以便他们无法利用我最初授予的电子邮件权限来编辑我帐户中的电子邮件内容.

Can I restrict people so that they can not edit the email content from my account by exploiting the email permissions which I had originally given it.

235325 只是要清楚一点,我不担心代码的正常运行.我只想要一种保护脚本的方法,这样对电子表格具有编辑"权限的流氓应该无法编辑我的脚本以发送他/她想要的任何电子邮件.

235325 Just to be clear, I am not worried about normal functioning of code. I just want a way of securing the script so that someone rogue with 'edit' permissions to the spreadsheet should not be able to edit my script to send any email that he/she wants .

推荐答案

通过创建未绑定脚本并调用以下全局变量来隔离代码:

Isolate the code by creating an unbound script and calling the following global variables:

var ss = SpreadsheetApp.openById("yourSheetIdHere");
var sheet = ss.getSheetByName("sheetName");
/* do more stuff */

这样,您是唯一运行脚本发送电子邮件的用户.当该脚本解除绑定时,它始终以您的身份运行,并且没有编辑权限的人无法打开该脚本.不过,您需要创建一个可以监视工作表的触发器. 来自Google Apps文档:

This way, you're the only user running the script to send the email. When the script is unbound, it always runs as you and cannot be opened by someone without edit rights. You'll need to create a trigger which watches the sheet, though. From the Google Apps docs:

function createSpreadsheetEditTrigger() {
  var ss = SpreadsheetApp.getActive();
  ScriptApp.newTrigger('myFunction')
      .forSpreadsheet(ss)
      .onEdit()
      .create();
}

然后,要确保没有人弄乱您的电子邮件,您可以选择两条路线中的一条,都使用Session.getActiveUser().

Then, to be super-sure no one is messing with your emails, you could take one of a couple routes, both using Session.getActiveUser().

方法一-以活动用户身份发送电子邮件.

Method One - Send email as the active user.

这很简单-使用Session.getActiveUser().getEmail()设置一个email变量,并将其传递到您的GmailApp方法中.这将设置要从图纸编辑器发送的电子邮件地址.

This is simple - set an email variable with Session.getActiveUser().getEmail() and pass that into your GmailApp method. This will set the email address to send from the sheet editor.

方法二-验证试图运行该功能的用户.

Method Two - Validate the user trying to run the function.

您还可以将经过身份验证的用户存储在脚本的数组中.使用Session.getActiveUser().getEmail()并检查数组是否包含电子邮件地址.如果是这样,请从身份验证器功能中选择return true并继续.如果没有,请传递错误消息.

You could also store authenticated users in an array in your script. Use Session.getActiveUser().getEmail() and check that the array contains the email address. If so, return true from the authenticator function and continue. If not, pass an error message.

此处是Google文档

这篇关于我可以阻止具有编辑权限的人修改我的Google Apps脚本吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆