添加要复制到功能的IF(背景色)条件>>

问题描述 投票:0回答:1

所以Tanaike以前很友好地为我创建了一个Copyto函数,它基本上为我所拥有的电子表格拍摄了快照,然后将其复制到新位置,而没有带来每个单元格的相应公式:

function onOpen() {
  var ui = SpreadsheetApp.getUi();
  // Or DocumentApp or FormApp.
  ui.createMenu('Master Menu')
      .addItem('Archive Report', 'Archiver')
      .addToUi();
}

function Archiver() {
  var spreadsheetId = "File_ID"; // Please set the source Spreadsheet ID.
  var destFolderId = "File_ID";  // Please set the destination folder ID.

  // Copy each sheet in the source Spreadsheet by removing the formulas as the temporal sheets.
  var ss = SpreadsheetApp.openById(spreadsheetId);
  var tempSheets = ss.getSheets().map(function(sheet, i) {
    var dstSheet = sheet.copyTo(ss).setName(sheet.getSheetName() + "_temp");
    var src = dstSheet.getDataRange();

   src.copyTo(src, {contentsOnly: true});


    return dstSheet;
  });

  // Copy the source Spreadsheet.
  var destination = ss.copy(ss.getName() + " - " + new Date().toLocaleString());

  // Delete the temporal sheets in the source Spreadsheet.
  tempSheets.forEach(function(sheet) {ss.deleteSheet(sheet)});

  // Delete the original sheets from the copied Spreadsheet and rename the copied sheets.
  destination.getSheets().forEach(function(sheet) {
    var sheetName = sheet.getSheetName();
    if (sheetName.indexOf("_temp") == -1) {
      destination.deleteSheet(sheet);
    } else {
      sheet.setName(sheetName.slice(0, -5));
    }
  });

  // Move file to the destination folder.
  var file = DriveApp.getFileById(destination.getId());
  DriveApp.getFolderById(destFolderId).addFile(file);
  file.getParents().next().removeFile(file);
}

我对此功能表示感谢,但如果可能的话,我想做的最后一件事:我要这样做,以便仅将突出显示为绿色的单元格(“#0D7813”)删除各自的公式(仅内容),而其他所有单元格的公式都将保留,而复制包含它们的工作表时。我尽了一切努力使它起作用,但是我不知道如何对此做一个布尔声明,这是我的尝试:

// Copy each sheet in the source Spreadsheet by removing the formulas as the temporal sheets.
  var ss = SpreadsheetApp.openById(spreadsheetId);
  var tempSheets = ss.getSheets().map(function(sheet, i) {
    var dstSheet = sheet.copyTo(ss).setName(sheet.getSheetName() + "_temp");
    var src = dstSheet.getDataRange();
    var backgrounds = tempSheets.getDataRange().getBackgrounds()

    if(backgrounds === "#0D7813"){
    src.copyTo(src, {contentsOnly: true});
    }
    else{
      src.copyTo(src, {contentsOnly: false});
    }
    return dstSheet;

我不知道如何为该过程的单个单元格设置布尔值,因为它似乎在复制整个工作表。任何帮助,将不胜感激,谢谢。

您去羔羊,谢谢您到目前为止给予的所有帮助:):

<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9WTkpjMi5wbmcifQ==” alt =“原始主控”>“>

“我正在使用”

“结果副本”

所以Tanaike以前很友好地为我创建了一个Copyto函数,它基本上为我所拥有的电子表格拍摄了快照,然后将其复制到了新位置,而没有带来各自的...

google-apps-script google-sheets google-sheets-api
1个回答
0
投票
[请记住,getBackgrounds返回二维颜色代码数组,而不仅仅是一个值。因此条件backgrounds === "#0D7813"永远不会返回true。您必须单独检查每个单元格。

在这种情况下,我会(1)使用公式按原样复制所有电子表格,然后(2)遍历每张工作表的每个单元格(包含内容),如果背景颜色是指定的一个。

© www.soinside.com 2019 - 2024. All rights reserved.