[英]Dynamically edit columns in google sheets
语境
我正在创建一个股票价格数据数据库。 我目前正在使用以下onEdit
function:
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 function 但它也没有用。
到目前为止,我一直在使用如下表格公式: 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);
}
}
问题:
您的if
语句中没有带有代码的右括号。
这里if(excludeCells.indexOf(cell)===-1)
有问题:
var excludeCells = ["M1","I1","I2"];
是一个字符串数组, var cell = e.range;
是一个范围 object。您实际上是在比较两个不同的东西(一个string
与一个range
object。)
相反,您想将if(excludeCells.indexOf(cell)===-1)
替换为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
列中有一个随机值,假设第900
行,那么900
将是工作表中的最后一行。 请小心,否则您将需要其他方法来获取最后一行内容。 公式也很丰富。 因此一直到工作表底部的公式可能会确定getLastRow
返回的内容。试试这个:
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
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.