【问题标题】:Determining Active Users on the same Google Spreadsheet在同一个 Google 电子表格上确定活跃用户
【发布时间】:2018-07-14 07:22:53
【问题描述】:

我有一个包含 许多 工作表的共享电子表格,其中的 OnOpen 脚本隐藏了所有打开的工作表,仪表板工作表除外。有一个自定义菜单,用户可以从中显示和处理工作表。

问题在于,当 User1 处理 Sheet1 时,User2 打开电子表格,而 OnOpen 脚本为 User1 和 User2 隐藏 Sheet1。

有没有办法确定其他用户是否当前正在处理电子表格? (控制 OnOpen 工作表隐藏操作)。

实现此目的的另一种方法?

【问题讨论】:

  • 在您的情况下,使用锁定服务怎么样? developers.google.com/apps-script/reference/lock
  • 用户仍然可以使用“查看”菜单来取消隐藏工作表——这不会触发锁定服务——我认为这必须从用户单击自定义菜单中触发。不幸的是,我不能相信用户的行为......
  • 真的很抱歉帮不上忙。
  • 相关问题:stackoverflow.com/questions/39602639/… 我说相关,因为您对“活动”的定义是那些正在编辑工作表的人,而不仅仅是查看。

标签: google-apps-script google-sheets


【解决方案1】:

我认为确定是否有活跃用户的最接近方法是使用捕获编辑的简单触发器不断缓存工作表上的活动:

var EDIT_CACHESTRING = "last write";
// Bind the simple edit trigger (no authorizations needed)
function onEdit(eventObject) {
  CacheService.getDocumentCache()
      .put(EDIT_CACHESTRING, (new Date().getTime()).toString());
}

function onOpen(eventObject) {
  // or function myNonSimpleTriggerOnOpen(e), if you do stuff that needs authorization.
  const cache = CacheService.getDocumentCache();

  // If nothing was cached, there have been no recent edits.
  // (default cache duration is 10 minutes).
  var lastWrite = cache.get(EDIT_CACHESTRING);
  var showOnlyDashboard = !lastWrite;

  // If something was cached, then determine how long it has been since the edit.
  if (lastWrite) {
    const now = new Date().getTime();
    lastWrite = parseInt(lastWrite, 10);
    showOnlyDashboard = (now - lastWrite) > someNumberOfSeconds * 1000;
  }
  if (showOnlyDashboard) {
    methodThatHidesSheets();
  }
  /**
   * other stuff you do in your on open function
   */
}

您可以变得更漂亮,并缓存一个包含已编辑工作表名称/ID(您将从编辑触发器的事件对象中获得)以及编辑时间的对象,以允许隐藏非活动工作表,即使其他正在修改。

另一种方法(也更复杂)是将Drive Activity API 与当前文档一起使用。您将在您选择的时间范围内检查某种活动,并在该时间范围内没有该活动时拨打methodThatHidesSheets()。

这两种方法都无法确定是否存在工作表的非活动查看器。正如您已经注意到的那样,无法针对每个用户控制同一文档中工作表的可见性 - 文档的任何当前查看者都与所有其他当前查看者看到相同的工作表。

必读:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-26
    • 1970-01-01
    • 2014-10-28
    • 2020-05-21
    相关资源
    最近更新 更多