Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/google-sheets/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google apps script Google Sheets:将一列单元格按条件格式化为相邻单元格(列)_Google Apps Script_Google Sheets - Fatal编程技术网

Google apps script Google Sheets:将一列单元格按条件格式化为相邻单元格(列)

Google apps script Google Sheets:将一列单元格按条件格式化为相邻单元格(列),google-apps-script,google-sheets,Google Apps Script,Google Sheets,我对编码非常陌生,没有真正尝试过任何东西。我确实看到,通过宏之外的条件格式,这是可能的,但它似乎只适用于当前的电子表格,我没有注意到有一种方法可以对每张工作表中所有适用的单元格快速执行此操作 我有一个用于预算编制的电子表格。B列标记为小计,这是D列(实际支出)应满足但不超过的金额。我希望D列中的单元格为默认颜色,直到它达到B列中相邻单元格的数量。如果数量超过相邻B列单元格中的数字,则我希望它为红色 这应该适用于工作簿中的每一张工作表,我希望它能够自动执行此操作,而不会提示打开工作簿以外的任何操作

我对编码非常陌生,没有真正尝试过任何东西。我确实看到,通过宏之外的条件格式,这是可能的,但它似乎只适用于当前的电子表格,我没有注意到有一种方法可以对每张工作表中所有适用的单元格快速执行此操作

我有一个用于预算编制的电子表格。B列标记为小计,这是D列(实际支出)应满足但不超过的金额。我希望D列中的单元格为默认颜色,直到它达到B列中相邻单元格的数量。如果数量超过相邻B列单元格中的数字,则我希望它为红色

这应该适用于工作簿中的每一张工作表,我希望它能够自动执行此操作,而不会提示打开工作簿以外的任何操作

另外,当我为那个支付期做预算时,如果我超出预算,我会将标签的颜色更改为红色,如果我在预算范围内,则标签为黑色

编码是我一直想学习的东西,我认为这是一个很好的开始

下面的链接是一个预算示例。“手机”价格过高,“汽油/燃料”价格正确,“杂货”价格过低

编辑:我算出了条件格式。
我的下一个问题是如何使其应用于所有工作表,而不仅仅是当前工作表?

我采用了录制宏的方法,同时应用了我想要的条件格式。结果很长很笨重,但我已将其简化为以下脚本,以实现我的目的:轻松有效。将此应用于工作簿中的所有电子表格会很酷,但键盘快捷键很容易使用。在使用手机上的每个电子表格之前,我只想这样做

function ZeroBalance1() {
  var spreadsheet = SpreadsheetApp.getActive();
   var conditionalFormatRules = 
spreadsheet.getActiveSheet().getConditionalFormatRules();

  conditionalFormatRules.splice(conditionalFormatRules.length - 1, 1, 
SpreadsheetApp.newConditionalFormatRule()
  .setRanges([spreadsheet.getRange('D3:D72')])
  .whenFormulaSatisfied('=D3>1*B3')
  .setBackground('#FF0000')
  .build());

  conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
  .setRanges([spreadsheet.getRange('D3:D72')])
  .whenCellEmpty()
   .build());

  conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
  .setRanges([spreadsheet.getRange('D3:D72')])
    .whenFormulaSatisfied('=D3=B3')
  .setBackground('#00FF00')
   .build());

  spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
};

我对您的代码进行了一些修改,并提出了一个脚本,该脚本可以在电子表格中的所有工作表中循环,并将条件格式一次添加到所有工作表中。基本上,您可以获得电子表格中所有工作表的数组,并且使用
for
循环可以为所有工作表创建规则:

function ZeroBalance() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheets = spreadsheet.getSheets();
  var column = 4;
  // Loop through each sheet in the file
  for(var i = 0; i < sheets.length; i++) {
    var sheet = sheets[i];
    var range = sheet.getRange('D3:D72');
    // Create conditional rules
    var turnRed = SpreadsheetApp.newConditionalFormatRule()
      .setRanges([range])
      .whenFormulaSatisfied('=D3>1*B3')
      .setBackground('#FF0000')
      .build();
    var turnDefault = SpreadsheetApp.newConditionalFormatRule()
      .setRanges([range])
      .whenCellEmpty()
      .build();
    var turnGreen = SpreadsheetApp.newConditionalFormatRule()
      .setRanges([range])
      .whenFormulaSatisfied('=D3=B3')
      .setBackground('#00FF00') // Green
      .build();
    var conditionalFormatRules = [turnRed, turnDefault, turnGreen];
    // Add conditional rules to the sheet
    sheet.setConditionalFormatRules(conditionalFormatRules);
  }
}
函数ZeroBalance(){
var电子表格=SpreadsheetApp.getActive();
var sheets=电子表格.getSheets();
var列=4;
//循环浏览文件中的每个工作表
对于(变量i=0;i1*B3')时
.挫折背景(“#FF0000”)
.build();
var turnDefault=SpreadsheetApp.newConditionalFormatRule()
.setRanges([range])
.whenCellEmpty()
.build();
var turnGreen=SpreadsheetApp.newConditionalFormatRule()
.setRanges([range])
.WhenFormula满足('=D3=B3')
.setBackground('#00FF00')//绿色
.build();
var ConditionalFormatures=[turnRed,turnDefault,turnGreen];
//向工作表中添加条件规则
表.设置条件格式(条件格式);
}
}
如果对你有效,请告诉我

function ZeroBalance() {
  var spreadsheet = SpreadsheetApp.getActive();
  var sheets = spreadsheet.getSheets();
  var column = 4;
  // Loop through each sheet in the file
  for(var i = 0; i < sheets.length; i++) {
    var sheet = sheets[i];
    var range = sheet.getRange('D3:D72');
    // Create conditional rules
    var turnRed = SpreadsheetApp.newConditionalFormatRule()
      .setRanges([range])
      .whenFormulaSatisfied('=D3>1*B3')
      .setBackground('#FF0000')
      .build();
    var turnDefault = SpreadsheetApp.newConditionalFormatRule()
      .setRanges([range])
      .whenCellEmpty()
      .build();
    var turnGreen = SpreadsheetApp.newConditionalFormatRule()
      .setRanges([range])
      .whenFormulaSatisfied('=D3=B3')
      .setBackground('#00FF00') // Green
      .build();
    var conditionalFormatRules = [turnRed, turnDefault, turnGreen];
    // Add conditional rules to the sheet
    sheet.setConditionalFormatRules(conditionalFormatRules);
  }
}