App脚本的onedit函数 – 非Google用户

huangapple go评论73阅读模式
英文:

App script onedit function - non-google users

问题

我有以下的代码:

function onEdit(e) {
  const sheetRegex = /^(Sheet1)$/i;
  const emailColumn = 1; 
  let sheet;
  if (!e
    || !e.range
    || e.range.columnStart <= emailColumn
    || !(sheet = e.range.getSheet()).getName().match(sheetRegex)) {
    return;
  }
  const emailCell = sheet.getRange(e.range.rowStart, emailColumn);
  if (!emailCell.getDisplayValue()) {
    SpreadsheetApp.getUi().alert("请填写有效的电子邮件地址");
    emailCell.activate();
  }
}

我正在尝试确保用户在填写电子表格时输入电子邮件地址。当用户登录到Google帐户时,它可以完美运行。如果用户使用除Chrome之外的其他浏览器或使用隐身模式,则不起作用。是否有任何解决方法?

英文:

I have the following code:

function onEdit(e) {
  const sheetRegex = /^(Sheet1)$/i;
  const emailColumn = 1; 
  let sheet;
  if (!e
    || !e.range
    || e.range.columnStart &lt;= emailColumn
    || !(sheet = e.range.getSheet()).getName().match(sheetRegex)) {
    return;
  }
  const emailCell = sheet.getRange(e.range.rowStart, emailColumn);
  if (!emailCell.getDisplayValue()) {
    SpreadsheetApp.getUi().alert(&quot;Please enter a valid email address&quot;);
    emailCell.activate();
  }
}

I am trying to insure that users enter an email address when filling in the spreadsheet. It works perfectly fine when the user is logged in to a google account. If the user uses another browser other than chrome or uses incognito mode, it does not work. Is there anyway around this?

答案1

得分: 0

很遗憾,如果您明确希望访问 UI(通过 SpreadsheetApp.getUi()),那么您将无法使其适用于未登录的用户。

我假设您正在使用一个简单的触发器(即只是使用保留的 onEdit 方法名称)。根据这个问题中的评论和Google的文档,简单触发器不会明确地从最终用户那里请求授权,但它们会在最终用户的授权下运行,这意味着它们需要正确登录。

您可以使用可安装的触发器,这使函数可以在您的授权下每次运行。您可以根据这些说明设置它(但基本上,您只需将函数重命名为 onEdit 以外的其他名称,并在 Apps Script 编辑器左侧的“触发器”选项卡下启用它)。不幸的是(或者对于安全原因来说是幸运的),如果您正在访问敏感/受限资源,这些触发器不会为未登录的用户运行,似乎包括 UI 元素(正如此处所讨论的)。

我确认了最简单的脚本(只是编辑另一个单元格)作为可安装触发器运行时适用于已登录用户、未登录用户和隐身模式,但 SpreadsheetApp.getUi() 仅适用于已登录用户。

根据您的用例,您可以修改您的脚本,使其(a)作为可安装触发器运行,(b)只执行简单的操作以提醒用户。不幸的是,对于最后一部分,我不能提供更具体的信息,因为我无法在 Google 的文档中找到相关信息,因此您可能需要进行一些试验,但也许只需在相邻单元格中放置大而粗的字母来警告用户可能就足够了。

英文:

So unfortunately if you explicitly want to access the ui (via SpreadsheetApp.getUi()), you will not be able to get this to work for non-logged in users.

I presume that you are using a simple trigger (ie just using the reserved onEdit method name). Per the comments in this question and Google's documentation, simple triggers do not request authorization explicitly from the end user, but they do run under the authorization of the end user, meaning that they need to be logged in properly.

You can use Installable Triggers, which allows the function to run under your authorization every time. You can find instructions on how to set that up according to these instructions (but essentially, you just rename the function to something other than onEdit and enable it under the Triggers tab on the left-hand side of the Apps Script Editor). Unfortunately (or fortunately for security reasons), these triggers will not run for not logged in users if you are accessing sensitive/restricted resources, which seems to include the UI elements (as was discussed here).

I confirmed that the simplest script (that just edits another cell) run as an installable trigger does work for logged in users, not logged in users, and in incognito mode, but the SpreadsheetApp.getUi() only works for logged in users.

Depending on your use case, you may be able to modify your script so that it (a) runs as an installed trigger and (b) only performs simple operations to alert the user. I unfortunately cannot be more specific on that last part, as I can't find any documentation from Google on this, so you may have to play around with it, but maybe just something like putting big bold letters in an adjacent cell to warn the user would be sufficient.

huangapple
  • 本文由 发表于 2023年7月17日 20:46:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76704598.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定