【问题标题】:How to use a script to copy conditional formatting rules from one sheet to another?如何使用脚本将条件格式规则从一张纸复制到另一张纸?
【发布时间】:2020-03-15 07:15:57
【问题描述】:

我正在尝试在 Google 表格中将条件格式规则从我的一张表格复制到其他表格。我知道,我可以复制并使用“特殊粘贴”来复制/粘贴它们,但我特别想使用脚本,因为我已经设置了一个自动创建新工作表的脚本,我想应用对我制作的所有工作表进行条件格式设置,因为整个电子表格是一种日志,并且包含条件格式的特定列适用于整个工作表。这是我所拥有的:

【问题讨论】:

  • 就个人而言,我认为将格式设置到电子表格中的最佳方法是创建一个电子表格,按照您想要的方式对其进行编辑,然后将其用作模板来创建其余部分。

标签: google-apps-script google-sheets google-sheets-conditionalformatting


【解决方案1】:
    function passConditionalFormat(sourceSheetName, targetSheetName) {
      var ss = SpreadsheetApp.getActive();
      //The source of data formatting rules
      var sourceSheet = ss.getSheetByName(sourceSheetName);
      var range = sourceSheet.getRange(1, 1, sourceSheet.getMaxRows(), sourceSheet.getMaxColumns());
      //The target of data formatting rules
      var targetSheet = ss.getSheetByName(targetSheetName);
      
      range.copyFormatToRange(targetSheet, 1, sourceSheet.getMaxColumns(), 1, sourceSheet.getMaxRows());
    }

您可以使用以下选项之一:

  1. copyFormatToRange(sheet, column, columnEnd, row, rowEnd)
  2. copyFormatToRange(gridId, column, columnEnd, row, rowEnd)

请注意,此解决方案仅适用于将条件格式从一个工作表传递到另一个工作表在同一电子表格中

【讨论】:

  • Avkeren,请为该方法及其签名的使用提供一些上下文(尽管可能很明显),其他人可以使用的示例也将是一个很好的补充,它将在帖子中有所帮助积极收到(特别是因为旧问题的新答案已进入同行评审队列)谢谢!
  • 谢谢你,Avkeren!请将先前的评论标记为不再需要以减少混乱。一个小提示 - 请不要添加无法在当前环境中运行的 sn-ps,代码围栏就可以了。无论如何 - 感谢您更新答案!
  • 我原以为 copyTo()PASTE_CONDITIONAL_FORMATTING (developers.google.com/apps-script/reference/spreadsheet/…) 可以解决问题。不幸的是,与其只粘贴应有的规则 - 它似乎从目标范围中删除了所有值和常规格式...
【解决方案2】:

使用 SpreadsheetApp functionsConditionalFormatRuleBuilder 类,您可以从工作表中的规则创建新规则并将它们插入新工作表中。我创建了以下代码,用于从“Merkler”表中获取所有规则并将它们插入到“新表”中,前提是这些规则应用于“F”列。

function copyFormat() {
  //Get the Spreadsheet where the Merkler and new sheet are in
  var spreadSheet = SpreadsheetApp.openById("[SPREADSHEET-ID]");
  //The source sheet with the formatting in column F is called Merkler
  var sheet = spreadSheet.getSheetByName("Merkler");
  //The target sheet is called "New Sheet"
  var newSheet = spreadSheet.getSheetByName("New sheet");
  
  //The formatting will be taken from column F and copy to it in new Sheet
  var range = newSheet.getRange("F2:F1000");
  var column = 6; //F is the 6th column

  //Get all Sheet rules and iterate through them
  var rules = sheet.getConditionalFormatRules();
  var newRules = [];
  
  for(var r = 0; r < rules.length; r++) {
    var rule = rules[r];
    //Get condition for each rule
    var booleanCondition = rule.getBooleanCondition();

    //Get the ranges to which each rule applies and iterate through
    var ranges = rule.getRanges();
    for (var i = 0; i < ranges.length; i++) {
      var ruleColumn = ranges[i].getColumn();  

      //If condition isn't null and edited column is the same as the one in the range, add rule
      if((ruleColumn == column) && (booleanCondition != null)) {
        var newRule = SpreadsheetApp.newConditionalFormatRule()
        .withCriteria(booleanCondition.getCriteriaType(), booleanCondition.getCriteriaValues())
        .setBackground(booleanCondition.getBackground())
        .setRanges([range])
        .build();
        newRules.push(newRule);
      }
    }
  }
  newSheet.setConditionalFormatRules(newRules);
}

您需要替换电子表格ID。

【讨论】:

  • 我还有一个脚本附加到不同页面上的一个按钮上,该按钮会自动创建一个新工作表,我必须如何更改您的代码才能为我工作? Screenshot
  • 在脚本中添加copyFormat函数并替换[Spreadsheet-ID]。阅读如何在 Apps 脚本中运行函数developers.google.com/apps-script/overview
  • 所以我不需要更改脚本中的任何内容,只需将其粘贴并更改 ID,一切都会正常运行吗?
  • 我复制了我的电子表格,更改了 ID,并对其进行了测试,它仍然制作了工作表,但不是条件格式。
  • 新建工作表后需要运行copyFormat函数。另外,检查我在代码中添加的 cmets 以了解它在做什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-03
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多