我对脚本很陌生,但是我需要为我的RPG游戏系统在Google页面脚本中做以下工作.
有人能帮我吗?
发布于 2020-02-12 18:13:40
根据我所看到的,您希望检查两个单元格中的最高值,并希望使用这个值在一个值表中得到一个准确的位置,并在另一个单元格上得到该值。
该问题可分三个阶段解决。1)在细胞中找到最高值。2)检索表中的值。3)用检索到的值设置目标单元格。
1)取决于是否在单元格中找到最高值,指的是:
A1 1例,A2 5例,A3 3例,A4 4例。并从A2细胞中提取5。
答:您的解决方案将使用Array.prototype.concat.apply() & Math.max.apply().和.split()的组合参考:Array.prototype.concat.apply() B:您的解决方案将使用getRange(行、列、行、列)和& 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
}
https://stackoverflow.com/questions/60190789
复制