无法在工作表的谷歌应用程序脚本中运行两个独立的函数



所以我正在创建一个工作表来跟踪一些数据,并通过比率和日期将其输入到图表中,例如,在这个特定的时间,我有10和5等于2的比率。我能够找到一个脚本来自动计算日期,但我也想自动计算比率,因为每次我都必须输入单元格引用/单元格引用。当然,它只是点击和拖动,但我希望它能像日期一样自动化。我一直在自学javascript,但我还是个新手。基本上是我想出的。

function onEdit() { 
var s = SpreadsheetApp.getActiveSheet(); 
if( s.getName() == "Overall", "M&K", "Con" ) {  
var r = s.getActiveCell();  
if( r.getColumn() == 2 ) { //checks that the cell being edited is in column B  
var nextCell = r.offset(0, -2);  
if( nextCell.getValue() === '' ) //checks if the adjacent cell is empty or not?  
nextCell.setValue(new Date()).setNumberFormat("dd/MM/yyyy, hh:mm");  
}  
}  
}  
function onEdit() {  
var s1 = SpreadsheetApp.getActiveSheet();  
if( s1.getName() == "Overall", "M&K", "Con" ) {  
var r1 = s1.getActiveCell();  
if( r1.getColumn() == 3 ) { //checks that the cell being edited is in column C  
var a = r1.offset(0, 1);  
var b = r1.offset(0, -1);  
var c = r1.offset(0, 0);  
if( a.getValue() === '' ) //checks if the adjacent cell is empty or not?  
a.setValue(b.getValue()/c.getValue());  
}  
}  
}

当我把这个放在我的一张纸上时,只有一张有效,而不是两者都有效,而且它总是最下面的一张,如果我试着把它放在第一位,什么都不会起作用。我确信这与剧本的结构有关,但我真的不知道。

我试着改变价值观,并尽我所知和所能对其进行重组,但我永远无法让两个同时运行,总是一个或另一个。

您有两个名称相同的函数,这通常是不允许的。谷歌应用程序脚本不知道该运行哪一个,所以它会抓住它看到的第一个。

您要做的是为这些函数中的每一个赋予唯一的名称,然后将它们设置为可安装触发器。

在项目的触发器部分(单击屏幕左侧的沙漏(,选择一个函数并将其设置为运行";从电子表格";以及";编辑时";。对每个函数都执行此操作,这样您就有了两个唯一的On Edit触发器。

项目中的每个函数都必须有一个唯一的名称。

这不起作用:

s.getName() == "Overall", "M&K", "Con"

只是为了踢球,我用它踢球,这似乎确实奏效了。不过,我不知道为什么。

s.getName() == "Overall" || "M&K" || "Con"

也许其他人可以解释。

这将是在不必使用可安装触发器的情况下获得功能的另一种方式。注意:在第二部分中,我没有仔细研究您的大部分逻辑。只是假设一切正常

function onEdit(e) {
const sh = e.range.getSheet();
if (sh.getName() == "Overall" || "M&K" || "Con" && e.range.columnStart == 2) {
const nextCell = e.range.offset(0, -2);
if (nextCell.getValue() === '') //checks if the adjacent cell is empty or not?  
nextCell.setValue(new Date()).setNumberFormat("dd/MM/yyyy, hh:mm");
}
if (sh.getName() == "Overall", "M&K", "Con" && e.range.columnStart == 3) {
var r1 = sh.getActiveCell();
var a = r1.offset(0, 1);
var b = r1.offset(0, -1);
var c = r1.offset(0, 0);
if (a.getValue() === '') {
a.setValue(b.getValue() / c.getValue());
}
}
}

我通常会这样写:

function onEdit(e) {
const sh = e.range.getSheet();
const shts = ["Overall", "M&K", "Con"];
const idx = shts.indexOf(sh.getName());
if (~idx) {
if (e.range.columnStart == 2) {
const nextCell = e.range.offset(0, -2);
if (nextCell.getValue() === '') 
nextCell.setValue(new Date()).setNumberFormat("dd/MM/yyyy, hh:mm");
}
if (e.range.columnStart == 3) {
var a = e.range.offset(0, 1);
var b = e.range.offset(0, -1);
var c = e.range.offset(0, 0);
if (a.getValue() === '') {
a.setValue(b.getValue() / c.getValue());
}
}
}
}

创建2个onEdit函数没有意义,相反,您应该将这2个函数打包为1,并根据条件运行代码。

对于您的第一个函数,当您在B列中工作时,您不可能从B列偏移(0,-2(,所以我将其更改为偏移量(0,-1(,这将导致A列。

对于第二个函数,我重命名了变量,因为你的"var a"实际上是D列。

最后,通过switch语句将这两个函数组合为一个函数。

function onEdit(e) { 
const s = SpreadsheetApp.getActiveSheet();
const sName = s.getName();
const boolean = [
sName == "Overall",
sName == "M&K",
sName == "Con"
]
if(boolean.some(check => !!check)) {
const r = s.getActiveCell();
switch (r.getColumn()) {
case 2:
const a = r.offset(0,-1);
if(a.getValue() === '') a.setValue(new Date()).setNumberFormat("dd/MM/yyyy, hh:mm");
break;
case 3:
const b = r.offset(0,-1);
const c = r.offset(0,0);
const d = r.offset(0,1);
if(d.getValue() === '') d.setValue(b.getValue()/c.getValue());
break;
default: return;
}
}  
}

最新更新