简体   繁体   English

如何使用脚本将工作表名称与数组元素进行比较并在 Google 工作表上执行任务?

[英]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);
    }
  }
}

Here is the logger output:这是记录器输出: 在此处输入图片说明

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.

相关问题 比较两个列中的两列A,并使用Google Apps脚本在表单1中生成唯一值 - Compare two columns A in two sheets and generate unique values in sheet 1 using Google Apps Script 用于发送具有不同工作表名称的工作簿数据的 copyTo Google 表格脚本 - copyTo Google Sheets Script for Send workbook data with different sheet names 如何使用脚本从 Google 表格中“工作表 B”上的数据生成“工作表 A”上的列表 - How Do You Generate List On "Sheet A" from Data on "Sheet B" in Google Sheets Using a Script 如何使用 Google Apps 脚本将 Google Drive 文件名记录到 Google Sheets - How to log Google Drive File Names to Google Sheets using Google Apps Script 使用 Google Apps 脚本,如何替换 Google 表格模板中的文本以制作新表格? - Using Google Apps Script, how can I replace text in a Google Sheets template to make a new Sheet? 比较日期谷歌表格脚本 - Compare dates google sheets script 将工作表 1 与工作表 2 进行比较并输出至工作表 3。Google 工作表。 JavaScript - Compare sheet 1 to sheet 2 and output to sheet 3. Google sheets. JavaScript 从带有脚本的工作表名称列表中排除工作表 - Exclude sheets from list Of Sheet Names With Script Google Apps 脚本 - 按列名而不是硬编码范围比较两张表的更改 - Google Apps Script - Compare two sheets for changes by column names instead of hardcoded ranges Google Sheets 脚本按工作表名称过滤 - Google Sheets script filter by sheet name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM