背景
我正在创建一个股票价格数据数据库。我目前正在使用以下 onEdit
函数:
function onEdit(e) {
// Get the spreadsheet that this will apply to, the tabs and columns
var ss = e.source.getActiveSheet();
var excludeTabs = ["Summary", "FTSE 250"];
var excludeColumns = [1,2,12,13,14,15,16]; // the columns to isolate for special reasons
var excludeCells = ["M1","I1","I2"];
// What is the criteria for action to be taken? If the terms defined in excludeTabs are met, then move onto the next criteria (indexOf used because we stored it as an array
if(excludeTabs.indexOf(ss.getName())===-1){
// The range from the spreadsheet.
// Scripts expects an event in a cell
var cell = e.range;
// For the entire column of this cell
var col = cell.getColumn();
// Within the universe of tabs that this script applies to, we want to exclude the columns in the array
if(excludeColumns.indexOf(col)===-1)
// Within the universe of tabs and columns, we want to exclude certain cells
if(excludeCells.indexOf(cell)===-1)
// Need to make sure it only applies to formulas
if(cell.getFormula() !== ""){
var destination = ss.getRange(4, col, ss.getLastRow()-1, 1);
cell.copyTo(destination);
}//End of the remaining universe of data taking out the exceptions
}//End Tab criteria
}//End function
这允许在我编辑单元格时对某些列进行编辑。到目前为止它可以工作,但有一些问题。
问题 1
有时,当我编辑列第四行上方的单元格时,尽管我告诉它从第四行开始,它也会编辑整列。这发生在几分钟前,我告诉它排除在“I2”以上的单元格中。我为此编写的代码有什么问题吗?
问题 2
我尝试为代码创建其他例外,其中对于某些指定的范围,它只会从不同的单元格范围进行编辑。不是每列的第四个单元格,而是说第 10 个单元格。我尝试将其添加到 var destination = ss.getRange(4, col, ss.getLastRow()-1, 1)
下方,但没有奏效。我还尝试为不同的单元格位置创建一个单独的 onEdit 函数,但它也不起作用。
到目前为止,我一直在使用如下表格公式:
IFERROR(IFS(C4="Returns","",AND(C4="",C5="",C6="",C7="",C8=""),"",AND(ISNUMBER(C4),ISNUMBER(C5),ISNUMBER(C6),ISNUMBER(C7),ISNUMBER(C8)),COVAR($C4:$C8,'FTSE 250'!J5:J9)),""))
但这只会变得一团糟。有时单元格中有数据,因此它会使上述公式无用。一个例子是下图。
更新 我希望 onEdit 从列的第 10 行开始并向下拖动,但仅限于该列(这是我将要编辑的该列中的行)。我还希望能够对其他列执行此操作(从不同行开始自动复制过程)。
[
更新 2
...
if(!excludeTabs.includes(ss.getName()) &&
!excludeColumns.includes(col) &&
!excludeCells.includes(cell.getA1Notation()) &&
cell.getFormula() !== ""
){
if(col==33){
var destination = ss.getRange(8, col, ss.getMaxRows()-7, 1);
cell.copyTo(destination);
}
else if(col===30){
var destination = ss.getRange(8, col, ss.getMaxRows()-7, 1);
cell.copyTo(destination);
}
else{
var destination = ss.getRange(4, col, ss.getMaxRows()-3, 1);
cell.copyTo(destination);
}
}
答案 0 :(得分:1)
问题:
您的 if
语句没有包含代码的右方括号。
这里if(excludeCells.indexOf(cell)===-1)
有问题:
var excludeCells = ["M1","I1","I2"];
是一个字符串数组,var cell = e.range;
是一个范围对象。您实际上是在比较两个不同的事物(string
与 range
对象。)
相反,您想要替换:if(excludeCells.indexOf(cell)===-1)
with if(excludeCells.indexOf(cell.getA1Notation())===-1)
。
改进:
不要使用多个 if
语句(最后导致一个代码块),而是使用一个带有多个条件的 if
语句。
同样,这个范围 getRange(4, col, ss.getLastRow()-1, 1);
也没有多大意义。使用 ss.getLastRow()-3
更有意义,因为您是从 3
开始的。
您可以像使用 excludeCells.indexOf(cell.getA1Notation())===-1
这样的 includes() 代替使用 !excludeCells.includes(cell.getA1Notation())
的长表达式。
function onEdit(e) {
var ss = e.source.getActiveSheet();
var excludeTabs = ["Summary", "FTSE 250"];
var excludeColumns = [1,2,12,13,14,15,16]; // the columns to isolate for special reasons
var excludeCells = ["M1","I1","I2"];
var cell = e.range;
var col = cell.getColumn();
if(!excludeTabs.includes(ss.getName()) &&
!excludeColumns.includes(col) &&
!excludeCells.includes(cell.getA1Notation()) &&
cell.getFormula() !== ""
){
if(col==33){
var destination = ss.getRange(10, col, ss.getMaxRows()-9, 1);
cell.copyTo(destination);
}
else{
var destination = ss.getRange(4, col, ss.getMaxRows()-3, 1);
cell.copyTo(destination);
}
}
}
请注意:
10
的最后一行的内容为 55
,但工作表末尾的 20
列中有一个随机值,假设行 { {1}} 那么 900
将是您工作表中的最后一行。小心这一点,否则您将需要其他方法来获取包含内容的最后一行。公式也是内容。因此,一直到工作表底部的公式可能会决定 900
返回的内容。答案 1 :(得分:1)
试试这个:
function onEdit(e) {
var sh = e.range.getSheet();
var excludeTabs = ["Summary", "FTSE 250"];
var excludeColumns = [1,2,12,13,14,15,16];
var excludeCells = ["M1","I1","I2"];
if(excludeTabs.indexOf(sh.getName())==-1 && excludeColumns.indexOf(e.range.columnStart)==-1 && excludeCells.indexOf(e.range.getA1Notation())==-1 && e.range.getFormula()!=""){
e.range.copyTo(sh.getRange(4, e.range.columnStart, sh.getLastRow()-3, 1));//The numbers of rows to the bottom is sh.getLastRow()-3 -1 will have to roll off of the bottom of the spreadsheet which will give you out of range errors
}
}
}