【发布时间】:2021-01-14 10:10:37
【问题描述】:
上下文
我正在创建一个股票价格数据数据库。我目前正在使用下面的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);
}
}
【问题讨论】:
标签: javascript google-apps-script google-sheets