google-apps-script - 检查最高位并从网格单元格复制文本
问题描述
我是脚本新手,但需要在我的 RPG 游戏系统的 Google 表单脚本中执行以下操作...
- 检查单元格中最高的一位数。即 1 6 3 2 4 = 6 最高数字。
- 在另一个单元格中检查相同的内容。
- 根据这些数字的组合,即 6-2、3-4、1-6 等,应复制另一张纸上网格表中特定单元格的文本。
- 网格在每个组合单元格中有 1 到 6 个垂直和 1 到 6 个水平的不同文本。
- 复制的文本应输入到第一页的特定单元格(即 E10)中。
有人能帮我吗?
解决方案
据我所知,您想检查两个单元格中的最大值,并希望使用它来获取值表中的确切位置,并将该值打印在另一个单元格上。
该问题可以分3个阶段解决。1) 找到单元格中的最大值。2) 检索表中的值。3) 使用检索到的值设置目标单元格。
1)根据您是否在单元格中找到最高值是指:
A) 1534 在一个单元格中并取 5 或 B) A1 中的 1、A2 中的 5、A3 中的 3 和 A4 中的 4。并从 A2 单元格中检索 5。
答:您的解决方案将使用.split() & Array.prototype.concat.apply() & Math.max.apply()的组合。参考:在 Google Apps 脚本 B 中将数组输入到 Math.Max :您的解决方案将使用getRange(row,column,row,column) & Array.prototype.concat.apply() & Math.max.apply()的组合
你会得到两个最大数字的输出,我们称之为 A 和 B
2) 要检索值,您需要getRange(A,B)和getValue()并且根据表的位置,您可能需要修改 A 和 B 以获得表中的正确位置。
3) 要设置单元格 E1 中的值,您需要setValue()函数。
您可能想尝试使用上面的句柄自行解决问题。它们可能不是最有效的,但应该有效。或者,我在下面为您提供了一个示例脚本。有一个缺陷,如果输入的值大于您的表,它不会标记出错误。所以要么限制用户输入,要么创建一种方法来标记错误:)
function SetRetrievedValue() {
var app = SpreadsheetApp;
var ss = app.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//var cellcontent1 = sheet.getRange(2,1,6,1).getValues(); use this if its a range of cells you are searching
var cell1 = sheet.getRange(1,1).getDisplayValue(); //gets value as string
var cellcontent1 = cell1.split(""); // splits up the string individually
var newcellcontent1 = Array.prototype.concat.apply([], cellcontent1); // flatten the array
var maxNum1 = Math.max.apply(null, newcellcontent1); //gets the max value in the array
// repeat of cell 1
var cell2 = sheet.getRange(1,2).getDisplayValue();
var cellcontent2 = cell2.split("");
var newcellcontent2 = Array.prototype.concat.apply([], cellcontent2);
var maxNum2 = Math.max.apply(null, newcellcontent2);
var tablecell = ss.getSheetByName("Table sheet").getRange(maxNum1,maxNum2).getValue(); //retrieve the value based on the corresponding max value
sheet.getRange(1,3).setValue(tablecell); // sets the cell C1 as the value retrieved from the table
}
推荐阅读
- c# - 如何知道玩家角色是否进入了触发器或其他带有碰撞器的对象?
- android - Kotlin 仅包装类
- excel - 您可以在 SUMIF 中使用 UDF 进行逻辑测试吗?
- oracle - 如何验证列在 PLSQL 中是否可以为空
- angular - 如何在 Angular 后调用中正确发送 application/x-www-form-urlencoded
- c# - 按字母顺序排列,Array.Sort() 效果不佳
- angular - Rxjs 将超时错误从管道抛出到可观察的错误部分
- xamarin.forms - 如何从 FirebaseMsgService 取消订阅以停止接收通知
- typescript - 是否有更简单的方法来查询 Cloud Firestore 以获取用户未见过的特定集合中的所有文档?
- .net - XAML - 如何将运行作为 ItemsControl ItemTemplate?