我有一个脚本,用于在两个电子表格中查找匹配的值。 “ ss1”和“ ss2”。我更改了代码,因为太容易混淆了。因此,我将其具体化了很多,但仍然一团糟。每当从源页面修改名称列表时,该函数就运行onChange()。然后,它将新名称列表与镜像表进行比较,以确定更改的内容。在将自身与镜像表进行比较时,它会在镜像表中添加或删除信息。
它使用一个循环,转到A,在Mirror表中找到A,是吗?转到B,在镜像表中找不到B?在“镜像”工作表中添加行,在“镜像”工作表中添加B,在“ B”行的主工作表的C列中添加一个下拉框(我将在此处解释一些句子)。再次开始循环,对吗?是。 B?是。 D吗镜像表中有C,主表中没有C。从镜像表中删除C删除行“ D”中C列的下拉列表框。重新开始于A,B,D ...等等。这样做时,它还会在主工作表中添加和删除列C,然后将其放入下拉框或将其删除。但是有时,当它删除主表上C列中的“下拉”框时,它也会删除C列直到最后一行,但保留该位置上方的所有内容。我不知道为什么要这么做。日志并没有说这样做,而是说只有一行。
有时候,它会添加或删除单行而不进行记录。让我觉得这是一个小故障,它在删除的同时将遍历循环的下一行。
在完成删除行的功能之前,它必须继续执行循环过程。有点像不小心按住删除按钮,因为它们在彼此之间互相看。
以下代码可让您了解我在说什么。
function checkAndPlace() {
...'Bunch of ss1 variables'...
var s1 = ss.getActiveSheet();
ss.getRange('F2').setValue('RUNNING')
var Avals = s1.getRange("A1:A").getValues();
var Alast1 = Avals.filter(String).length;
var s1LastRow = s1.getLastRow()+1;
var Sheet1DataRange = s1.getRange(1,1,s1LastRow,1);
var Sheet1BDataRange = s1.getRange(1,2,s1LastRow,1);
var Sheet1Data = Sheet1DataRange.getValues();
var Sheet1BData = Sheet1BDataRange.getValues();
var Sheet1length = Sheet1Data.length;
...'Bunch of ss2 variables'...
var s2 = ss2.getActiveSheet();
var Avals2 = s2.getRange("A1:A").getValues();
var Alast2 = Avals2.filter(String).length;
var s2LastRow = s2.getLastRow()+1;
var Sheet2DataRange = s2.getRange(1,1,s1LastRow,1);
var Sheet2BDataRange = s2.getRange(1,2,s1LastRow,1);
var Sheet2Data = Sheet2DataRange.getValues();
var Sheet2BData = Sheet2BDataRange.getValues();
var Sheet2length = Sheet2Data.length;
for (var i = 0; i < Sheet1Data.length; i++) {
var s1data = Sheet1Data[i][0];
var s1Bdata = Sheet1BData[i][0];
var s2data = Sheet2Data[i][0];
var s2Bdata = Sheet2BData[i][0];
if (s1data + s1Bdata != s2data + s2Bdata){
var falseRow = i+1
var s1falseRowARange = s1.getRange(falseRow,1,1,1);
var s1falseRowAName = s1falseRowARange.getValues();
var s1falseRowBRange = s1.getRange(falseRow,2,1,1);
var s1falseRowBName = s1falseRowBRange.getValues();
var s2falseRowARange = s2.getRange(falseRow,1,1,1);
var s2falseRowAName = s2falseRowARange.getValues();
var s2falseRowBRange = s2.getRange(falseRow,2,1,1);
var s2falseRowBName = s2falseRowBRange.getValues();
for (var j = 0; j < Sheet1Data.length; j++) {
var s1data = Sheet1Data[j][0];
var s1Bdata = Sheet1BData[j][0];
var s2data = Sheet2Data[j][0];
var s2Bdata = Sheet2BData[j][0];
if(s1falseRowAName + s1falseRowBName == s2data + s2Bdata){
var s2trueRow = j+1;
if(falseRow > s2trueRow) {
var fm2t = 'true';
}
else if(falseRow < s2trueRow) {
var fl2t = 'true';
}
var s2Rowfound = 'true';
}
else if(j+1 == s2LastRow && s2Rowfound != 'true'){
var fnot2t = 'true';
}
if(s2falseRowAName + s2falseRowBName == s1data + s1Bdata) {
var s1trueRow = j+1;
if(falseRow > s1trueRow) {
var fm1t = 'true';
}
else if(falseRow < s1trueRow){
var fl1t = 'true';
}
var s1Rowfound = 'true';
}
else if(j+1 == s1LastRow && s1Rowfound != 'true') {
var fnot1t = 'true';
}
if(j+1 == s1LastRow && fnot1t == 'true'){
var falseRow = i+1
...'Bunch of ss2 variables'...
var s2 = ss2.getActiveSheet();
s2.deleteRow(falseRow)
...'Bunch of ss1 variables'...
var s1 = ss.getActiveSheet();
deleteCell('C'+ falseRow)
checkAndPlace()
}
else if(j+1 == s1LastRow && fnot2t == 'true' && fl1t == 'true') {
var falseRow = i+1
...'Bunch of ss1 variables'...
var s1 = ss.getActiveSheet();
var validationList = SpreadsheetApp.newDataValidation().requireValueInList(['Values'], true).build()
insertCell('C' + falseRow)
setValue('C' + falseRow, 'Value')
var SRange = s1.getRange(falseRow,1,1,2);
var A1Range = SRange.getA1Notation();
var SData = SRange.getValues();
...'Bunch of ss2 variables'...
var s2 = ss2.getActiveSheet();
s2.insertRows(falseRow)
s2.getRange(A1Range).setValues(SData);
checkAndPlace()
}
else if(j+1 == s2LastRow && fnot2t == 'true' && fm1t == 'true') {
Logger.log('Don\'t add row '+falseRow+' start from beginning.');
}
else if(j+1 == s1LastRow && fm2t == 'true' && fm1t == 'true') {
Logger.log('How did this happen?!');
}
else if(j+1 == s1LastRow && fm2t == 'true' && fl1t == 'true') {
Logger.log('Chances are someone was added before row '+falseRow+'. Start from beginning.');
checkAndPlace()
}
else if(j+1 == s1LastRow && fl2t == 'true' && fm1t == 'true') {
Logger.log('Chances are someone was deleted before row '+falseRow+'. Start from beginning.');
checkAndPlace()
}
else if(j+1 == s1LastRow && fl2t == 'true' && fl1t == 'true') {
Logger.log('How did this happen?!');
}
}
return false;
}
}
s1.getRange('F2').setValue('')
}
function insertCell(cellRange) {
...'Bunch of ss1 variables'...
var s1 = ss.getActiveSheet();
s1.getRange(cellRange).insertCells(SpreadsheetApp.Dimension.ROWS);
}
function deleteCell(cellRange){
...'Bunch of ss1 variables'...
var s1 = ss.getActiveSheet();
s1.getRange(cellRange).deleteCells(SpreadsheetApp.Dimension.ROWS);
}
对不起,您必须看到所有这些。但是,以防万一整个代码中有某种原因,我决定展示一下。我确实修改了一些值的名称,例如id名称。
那么有谁知道如何防止它“持有删除键”?有什么办法可以让它在删除下一行之前查看if函数?不知道如何解决它,这让我发疯,使我感到我无能为力,可以防止不应该发生的事情。