谷歌工作表单元格重新计算

2023-12-03

我正在创建一款棋盘游戏,并决定为此目的选择 Google 表格。我已将问题简化为由一张纸和一个脚本组成的最小示例。


情况

Following points refer to my dice sheet:

  • Cells B2:C5 contain available dices. First column contains dice sprites, second comma-separated dice face numbers.
    • 游戏设计视角:艺术家可以更改骰子图像。设计者可以改变骰子面的数字。两种类型的更改都会自动传播到引用骰子的单元格。
  • Cells E2:I2 and E10:I10 contain particular throw. In each throw there's 1 to 5 references to dices in column B.
    • 游戏设计视角:有许多不同的游戏项目,每个项目可能都有不同的骰子来确定行动的结果。设计者可以添加或删除对骰子的引用,它将触发特定单元格的自动重新计算(在我们的例子中,这些单元格是K2 and K10).
  • Cells beginning at K2 and K10 hold the result of the DICEFACES function applied to the ranges E2:I2 and E10:I10.
    • 游戏设计视角:骰子面矩阵将进一步用于计算骰子概率。为了简单起见,我将矩阵本身视为最终结果。
  • DICEFACES是我在脚本编辑器中创建的自定义函数 文件Code.gs与样式表相关联。它返回一个矩阵 与所提供范围内的骰子相对应的骰子面。它是 正文如下:

    function DICEFACES(unused_ref_to_range_containing_dices)
    {
      var app  = SpreadsheetApp;
      var spr  = app.getActiveSheet();
    
      // In the end this array will hold the dice faces. For example two
      // 1d6 dices would result in [[1,2,3,4,5,6],[1,2,3,4,5,6]].
      //
      var Dices = [];
    
      // The the formula inside the active cell (i.e. the cell on which
      // we are calling this function). This is a string like:
      //
      // "=DICEFACES(E2:I2)"
      //
      var active_formula = spr.getActiveRange().getFormula();
    
      // Set item_range to the one pointed to by the formula. This could
      // be a range like E2:I2.
      //
      var item_range = spr.getRange(active_formula.match(/=\w+\((.*)\)/i)[1]);
    
      // Loop over dice cells in the item_range.
      //
      for (var i = 1; i <= item_range.getNumColumns(); i++)
      {
        // "=B2", "=B3", ...
        //
        var dice_formula = item_range.getCell(1, i).getFormula();
    
        // As soon as we encounter an empty formula, we skip (i.e. there are
        // no more dices).
        //
        if (dice_formula == "")
        {
          break;
        }
    
        // A reference to the cell containing the dice image. We don't really
        // need the image, the dice faces are of greater importance to us.
        //
        var dice_cell = spr.getRange(dice_formula.substr(1));
    
        // Move one column to the right prior to the dice_cell and retreive
        // the value of the cell. This is a string like "1,2,3,4,5,6".
        //
        var dice_csv = dice_cell.offset(0, 1).getValue();
    
        // Convert the CSV string to a javascript array like [1,2,3,4,5,6]
        // and push it to Dices.
        //
        Dices.push(dice_csv.split(",").map(Number));
      }
      return Dices;
    }
    

Problem

问题是当我改变列中的骰子面时C, the DICEFACE公式不会被重新计算。就在我创建之前截屏我添加了,4单元格的后缀C2正如你所看到的,没有4在细胞内N2。然而,如果我either重新保存Code.gs脚本文件or改变骰子E2:I2,重新计算立即发生。

我很确定我知道问题出在哪里:因为我正在遍历脚本中的单元格,所以工作表应用程序本身看不到列中单元格之间的引用链接C和公式K2 and K10。看看我的工作表,单元格引用可能类似于:

K4  <-- E2:I2   <-- B2, B3 (C is not here)
K10 <-- E10:I10 <-- B4, B5 (C is not here)

我的符号的含义A <-- B is If there's a change in range B, update cell A.


Question

我应该更改什么才能使修改骰子面后立即自动重新计算?如果这是不可能的,那么完成我的任务的最佳方法是什么?


问题是当我改变列中的骰子面时C, the DICEFACE公式不会被重新计算。

DIFACE 是自定义函数,当打开电子表格且自定义函数参数值发生更改时,会重新计算自定义函数。

考虑到上述情况,为了最大限度地减少对自定义函数的更改,请添加第二个参数作为触发器。

改变正则表达式

/=\w+\((.*)\)/i

to

/=\w+\((.*),.*\)/i

然后通过以下方式调用您的自定义函数

=DICEFACES(E2:I2,C2)

or

=DICEFACES(E2:I2,C2:C5)


I used a comma assuming that it's the Google Sheets argument separator being used but some spreadsheets instead could use a semicolon.

OP自定义功能修改版

/**
 * Returns a matrix of dice faces corresponding to the dices in the provided range.
 *
 * @param {Array} unused_ref_to_range_containing_dices Reference to range. i.e. E2:I2
 * @param {String|Number|Date|Array} ref_as_trigger Reference to a range used as trigger. i.e. C2 or C2:C5
 * @return array
 * @customfunction
 */
function DICEFACES(unused_ref_to_range_containing_dices,ref_as_trigger)
{
  var app  = SpreadsheetApp;
  var spr  = app.getActiveSheet();

  // In the end this array will hold the dice faces. For example two
  // 1d6 dices would result in [[1,2,3,4,5,6],[1,2,3,4,5,6]].
  //
  var Dices = [];

  // The the formula inside the active cell (i.e. the cell on which
  // we are calling this function). This is a string like:
  //
  // "=DICEFACES(E2:I2)"
  //
  var active_formula = spr.getActiveRange().getFormula();

  // Set item_range to the one pointed to by the formula. This could
  // be a range like E2:I2.
  //
  var item_range = spr.getRange(active_formula.match(/=\w+\((.*),.*\)/i)[1]); // CHANGED

  // Loop over dice cells in the item_range.
  //
  for (var i = 1; i <= item_range.getNumColumns(); i++)
  {
    // "=B2", "=B3", ...
    //
    var dice_formula = item_range.getCell(1, i).getFormula();

    // As soon as we encounter an empty formula, we skip (i.e. there are
    // no more dices).
    //
    if (dice_formula == "")
    {
      break;
    }

    // A reference to the cell containing the dice image. We don't really
    // need the image, the dice faces are of greater importance to us.
    //
    var dice_cell = spr.getRange(dice_formula.substr(1));

    // Move one column to the right prior to the dice_cell and retreive
    // the value of the cell. This is a string like "1,2,3,4,5,6".
    //
    var dice_csv = dice_cell.offset(0, 1).getValue();

    // Convert the CSV string to a javascript array like [1,2,3,4,5,6]
    // and push it to Dices.
    //
    Dices.push(dice_csv.split(",").map(Number));
  }
  return Dices;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

谷歌工作表单元格重新计算 的相关文章

随机推荐

  • 多个三元运算符

    我需要一些关于三元运算符的语法帮助 这将帮助我将正确的标记图标放在我的好地图上 我有三个区域 0 1 和 2 它们具有独特的图标 0 1 和 2 我以前只有两个区域 所以这个三元运算符工作得很好 var icon area 1 icon1
  • 在 Projection.Conditionals 中添加多个条件以进行查询

    我正在尝试写一个包含多个的案例when条款 像这样的东西 case when starks then 1 when wildlings then 2 when lannisters then 3 Else 0 End 我以前做过一个条件条件
  • GeventSocketIOWorker 没有属性“socket”

    我需要使用 Gunicorn 运行 Django 应用程序 我阅读了文档 我想我已经按照我应该的方式设置了所有内容 但是当我运行时 gunicorn worker class socketio sgunicorn GeventSocketI
  • Date.addingTimeInterval(_:) 和 Date.advanced(by:) 有什么区别?

    基金会的Datestruct 提供了两者Date addingTimeInterval and Date advanced by 它们在功能上似乎相同 但显然是不同的方法 它们之间实际上有区别还是最终相同 查看文档 advanced by
  • 使用 Tensorflow-Hub 中的 ELMo 时内存消耗大幅增加

    我目前正在尝试比较数百万个文档的相似性 对于 CPU 上的第一次测试 我将它们减少到每个字符大约 50 个 并尝试一次为其中 10 个字符获取 ELMo 嵌入 如下所示 ELMO https tfhub dev google elmo 2
  • Mockito、JUnit 和 Spring

    我今天才开始了解Mockito 我编写了一些简单的测试 使用 JUnit 见下文 但我不知道如何在 Spring 的托管 bean 中使用模拟对象 什么是最佳实践用于与 Spring 一起工作 我应该如何向我的 bean 注入模拟依赖项 你
  • 找不到 Microsoft.AspNet.SignalR.Server 3.0.0-beta7

    我在引用包时遇到问题 Microsoft AspNet SignalR Server 3 0 0 beta7 我唯一可以安装的是 测试版 5 版本 我在网上看到过其他例子 人们do引用这个 beta7 包 但我似乎找不到它 下面是我当前的
  • gdb 如何启动汇编编译的程序并一次执行一行?

    Valgrind 在他们的文档页面上说了以下内容 然后 您的程序将在 Valgrind 核心提供的合成 CPU 上运行 然而GDB似乎并没有这样做 它似乎启动了一个独立执行的单独进程 据我所知 也没有c 库 这就是我所做的 使用 clang
  • 删除sequelize返回值中的连接表数据

    我目前正在尝试删除一个joint检索关联数据时添加的表数据 查询是通过使用通过指定模型关系添加到模型的方法的sequelize来完成的 sequelize magic methods 由于某种原因 我无法做到这一点 我目前已尝试传递attr
  • Python 替换单引号(撇号除外)

    我正在对单词列表执行以下操作 我从古腾堡项目文本文件中读取行 用空格分割每一行 执行一般标点符号替换 然后在其自己的行上打印每个单词和标点符号标签 以便稍后进一步处理 我不确定如何用标签替换每个单引号或排除所有撇号 我当前的方法是使用编译的
  • SQL Server 2005:从 WHERE 子句调用存储过程

    我需要在 WHERE 子句中调用存储过程来进行 SELECT 应该是这样的 选择不同的前 10 名 i x d droit 来自 v droit d v info i WHERE d nomdroit yy AND i id2 AND 从
  • 转移矩阵

    考虑以下数据框 df data frame cusip paste A 1 10 sep xt c 1 2 3 2 3 5 2 4 5 1 xt1 c 1 4 2 1 1 4 2 2 2 5 数据分为五个州 哪个是分位数事实上 1 2 3
  • 无法读取未知加载命令0x80000022

    在我的应用程序中 我使用了大量从 Apple 的 SpeakHere 示例中复制的代码 当我在 iPhone 设备上运行该应用程序时 它在加载 XIB 之前会抛出此错误大约一百次 unable to read unknown load co
  • 如何在 Tomcat 上使用 JPA、Hibernate 和 Spring 避免类加载器泄漏

    The 打开 J2EE Web 模板是一个展示应用程序 wicket 在 Tomcat7 servlet 容器上运行的带有 Spring 和 Hibernate 的 JPA 它的 Maven 构建脚本似乎以标准方式使用组件 但是 当从 To
  • MinGW、MinGW-w64 和 MinGW-builds 之间有什么区别?

    两者有什么区别MinGW 明GW w64 and MinGW 构建 我应该使用哪一个在 Windows 8 机器上使用 Eclipse IDE 编译 c 11 源代码 MinGW 是 Windows 的 GCC 端口 并非所有 Window
  • 连续解雇2个ViewController

    我尝试了两种方法来连续解雇 2 个视图控制器 但只有其中一个被解雇 而不是第二个 method1 void LoginDone NSNotification notif self dismissViewControllerAnimated
  • 从图库中获取图像并在 ImageView 中显示

    我需要通过单击按钮从图库中获取图像并将其显示到图像视图中 我按照以下方式进行 btn image button setOnClickListener new View OnClickListener Override public void
  • 如何将文件从 DOS 转换为 Unix [关闭]

    Closed 这个问题不符合堆栈溢出指南 目前不接受答案 我需要在 PowerShell 中将文件从 DOS 转换为 Unix 我知道这在 Unix 中可以很容易地完成 dos2unix file newfile 它可以很简单 Get Co
  • 在java中在很短的时间内搜索一个非常大的ARPA文件

    我有一个将近 1 GB 的 ARPA 文件 我必须在不到 1 分钟的时间内完成搜索 我搜索了很多 但还没有找到合适的答案 我想我不必阅读整个文件 我只需跳转到文件中的特定行并读取整行 ARPA 文件的行长度不同 我不得不提一下 ARPA 文
  • 谷歌工作表单元格重新计算

    我正在创建一款棋盘游戏 并决定为此目的选择 Google 表格 我已将问题简化为由一张纸和一个脚本组成的最小示例 情况 Following points refer to my dice sheet Cells B2 C5 contain