如何找到具有特定背景颜色的单元格,并设置与之相关的其他单元格的背景? [英] How do I find the cell with a certain background color, and set background of different cell in relation to it?

查看:88
本文介绍了如何找到具有特定背景颜色的单元格,并设置与之相关的其他单元格的背景?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个代码,以在电子表格中找到红色单元格,然后将其向上移动一个单元格.这是我所拥有的:

I am trying to make a code that finds the red cell on the spreadsheets, and moves it up one cell. Here is what I have:

var ymax = 23;
var xmax = 23;
var playerx = 0;
var playery = 0;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheets()[0];
function moveup(){
findplayer();
sheet.getRange(playerx, playery + 1).setBackground('red');
sheet.getRange(playerx, playery).setBackground('white');
}
function findplayer(){
for(var x = 1; x < xmax; x++)
  for(var y = 1; y < ymax; y++)
  {
    var cell = sheet.getRange( 'a1:z23' ).getCell(x, y).getBackground();
    if(cell = 'red'){
      var playerfound = true;
      playerx = x;
      playery = y;

    }
  }
if (playerfound = false)
{
  findplayer();
  }
 }
 function onOpen() {
 var ui = SpreadsheetApp.getUi();
 ui.createMenu('controol')
   .addItem('up', 'moveup')
   .addToUi();
 }

由于某种原因,它没有从T10移动红色方块,而是在W22处创建了一个新方块.

For some reason instead of moving the red square from T10, it just makes a new one at W22.

我在做什么错了?

我也要求权利

推荐答案

以下比较有错误:

cell = 'red'


playerfound = false

它们使用JavaScript中的一个等号来给变量赋值,而应该使用==(抽象等式)或===(严格等式).

They use a single equality sign wich in JavaScript is used to assign a value to a variable, instead it should use == (abstract equality) or === (strict equality).

另一方面,getBackground()返回颜色代码,而不是颜色名称,因此使用#ff0000代替红色.

By the other hand, getBackground() returns the color code, not the color name, so instead of red use #ff0000.

这篇关于如何找到具有特定背景颜色的单元格,并设置与之相关的其他单元格的背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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