[英]How to compare Sheet Names with Array elements and execute tasks on Google Sheets using script?
The goal is to get each sheet name and check if it's not in the array.目标是获取每个工作表名称并检查它是否不在数组中。 If not, then certain ranges are copied into a "database" sheet and then compare the next sheet and so on.
如果不是,则将某些范围复制到“数据库”表中,然后比较下一个表,依此类推。
The problem is that itis comparing, it is giving me -1
as the result and it is executing the tasks, when it shouldn't be and I can't see where the flaw is.问题是比较,它给了我
-1
作为结果并且它正在执行任务,而它不应该是并且我看不到缺陷在哪里。
Here's the code:这是代码:
function concatenarResumos() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var databaseSheet = ss.getSheetByName("Database");
var processedSheetsRng = ss.getSheetByName("Suporte").getRange("F6:F").getValues().flat();
var shts = ss.getSheets();
for (var j = 0; j < shts.length; j++) {
for (var n = 0; n < processedSheetsRng.length; n++) {
if (processedSheetsRng[n].indexOf(shts[j].getSheetName() === -1)) {
shts[j].activate;
var sheetName = shts[j].getSheetName();
Logger.log("Sheet Name: " + sheetName);
Logger.log("Processed Sheet Name: " + processedSheetsRng[n])
var data = shts[j].getRange("C4").getValue();
var naoConforme = shts[j].getRange("B8:G12").getValues();
shts[j].getRange("B8:G12").copyTo(databaseSheet.getRange(databaseSheet.getLastRow() + 1, 2, 6, naoConforme.length), { contentsOnly: true });
var targetCol = databaseSheet.getRange('A:A').getValues();
var maxIndex = targetCol.reduce(function (maxIndex, row, index) {
return row[0] === "" ? maxIndex : index;
}, 0);
databaseSheet.getRange(maxIndex + 2, 1, naoConforme.length, 1).setValue(data);
}
}
}
function concatenarResumos() {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const dsh = ss.getSheetByName("Database");
const ssh = ss.getSheetByName("Suporte");
const svs = ssh.getRange(6, 6, sh.getLastRow() - 5).getValues().flat();
const shts = ss.getSheets();
for (let j = 0; j < shts.length; j++) {
for (let n = 0; n < svs.length; n++) {
if (~svs[n].indexOf(shts[j].getName())) {
let d1 = shts[j].getRange("C4").getValue();
shts[j].getRange("B8:G12").copyTo(dsh.getRange(dsh.getLastRow() + 1, 2, 6, d2.length), { contentsOnly: true });
let tcol = dsh.getRange(1, 1, dsh.getLastRow()).getValues();
let maxIndex = tcol.reduce(function (maxIndex, row, index) {return row[0] === "" ? maxIndex : index;}, 0);
dsh.getRange(maxIndex + 2, 1, d2.length, 1).setValue(d1);
}
}
}
Ranges like F6:F
& A:A
are bad to use with google apps script because they create a lot of nulls at the end of the array which need to get filtered out.像
F6:F
& A:A
这样的范围不适合与谷歌应用程序脚本一起使用,因为它们在数组的末尾创建了很多需要过滤掉的空值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.