Google Spreadsheet脚本,用于闪烁一系列单元格 [英] Google Spreadsheet Script to Blink a range of Cells

查看:155
本文介绍了Google Spreadsheet脚本,用于闪烁一系列单元格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是脚本新手,最近发现以下脚本可以在编辑时使单元格闪烁或更改颜色。我想实现相同的脚本,但要用于一系列单元格。我已经尝试过:A7:A,但是它不起作用。我相信我在某个地方缺少论点。

I'm New in scripting, Recently found the below script to make a cell flash or change color when edit. I would like to implement the same script but for a range of cells. I have tried ie: A7:A but it won't work. I believe I'm missing an argument somewhere.

function onEdit(e)
{
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var mysheet = ss.getSheetByName("Sheet1");
  var activeCell = ss.getActiveCell().getA1Notation();

  if( activeCell == "A1" )
  {
    for(var i=0;i<50;i++)
    {
      if( i%2 == 0 )
        mysheet.getRange("A1").setBackground("RED");
      else
        mysheet.getRange("A1").setBackground("WHITE");

      SpreadsheetApp.flush();
      Utilities.sleep(500);
    }
  }
}


推荐答案

我知道您希望编辑的单元格在A7:A范围内闪烁。这是通过事件对象完成的,其中range属性引用到有效范围。

I understand you want the edited cell to flash if it's in the range A7:A. This is done below using the event object in which the range property refers to the active range.

如果列为1,行> = 7,则闪烁50次。 (顺便说一下,这意味着闪烁25秒。)

If the column is 1 and row >= 7, flash 50 times. (This means 25 seconds of flashing, by the way.)

function onEdit(e) {
  if (e.range.getColumn() == 1 && e.range.getRow() >= 7) {
    for (var i = 0; i < 50; i++) {
      e.range.setBackground(i % 2 ? "WHITE" : "RED");
      SpreadsheetApp.flush();
      Utilities.sleep(500);
    }
  }
}

如果要整个范围A7:A都会闪烁,当其单元格中的任何一个被编辑时,相关的方法是 setBackgrounds ,并且需要使用双色数组。首先需要准备好该数组,因此代码变为

If you wanted the entire range A7:A to flash when any of its cells is edited, then the relevant method is setBackgrounds, and it takes a double array of colors. This array needs to be prepared first, so the code becomes

function onEdit(e) {
  if (e.range.getColumn() == 1 && e.range.getRow() >= 7) {
    var range = e.range.getSheet().getRange("A7:A");
    var height = range.getHeight();
    var whiteArray = Array.apply(null, Array(height)).map(function() {
      return ["WHITE"];
    });
    var redArray = Array.apply(null, Array(height)).map(function() {
      return ["RED"];
    });
    for (var i = 0; i < 50; i++) {
      range.setBackgrounds(i % 2 ? whiteArray : redArray);
      SpreadsheetApp.flush();
      Utilities.sleep(500);
    }
  }
}

使用数组填充方法来自此答案

using the array-filling method from this answer.

这篇关于Google Spreadsheet脚本,用于闪烁一系列单元格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆