简体   繁体   English

如何在Google Apps脚本中引用单元格

[英]How to Reference Cells in Google Apps Scripts

I'm transitioning from knowing VBA to Scripts for spreadsheet management. 我正在从了解VBA过渡到用于电子表格管理的脚本。 I created a function to flag cells that contain certain words within my spreadsheet. 我创建了一个函数来标记电子表格中包含某些单词的单元格。

How do I reference specific cells as it relates the overall code: 我如何引用特定的单元格,因为它与整体代码相关:

The issue is with the following being undefined. 问题在于未定义以下内容。

setBackgroundColor("#99CC99") setBackgroundColor(“#99CC99”)

Would this be appropriate?: 这合适吗?

values[i][0].setBackgroundColor("#99CC99") 值[i] [0] .setBackgroundColor(“#99CC99”)

Code below: 代码如下:

function foo(){
  var range = SpreadsheetApp.openById("1oh5wXFi4YM1bDvfF27pu1qHDKQEhn02drtfsrRgxArQ").getSheetByName("General").getRange("D:D");
var values = range.getValues();
for(var i in values){
  if(values[i][0].match("Rensselaer")!=null){
     setBackgroundColor("#99CC99")
  } 
  if(values[i][0].match("Albany")!=null){
     setBackgroundColor("#99CC99")
  }
  if(values[i][0].match("Saratoga")!=null){
     setBackgroundColor("#99CC99")
  }
}
}

setBackgroundColor(color) is a method of the Class Range, in other words, first you should get a range object then you could call that setBackgroundColor(color) for that object setBackgroundColor(color)是类范围的一种方法,换句话说,首先您应该获得一个范围对象,然后可以为该对象调用该setBackgroundColor(color)

values[i][0] isn't an appropriate way to reference a range because this variable holds cell content not the cell itself. values[i][0]不是引用范围的适当方法,因为此变量保存单元格内容而不是单元格本身。

There are a several ways to reference a range. 有几种引用范围的方法。 The basics forms are A1 Notation, and row and column numbers. 基本形式是A1表示法以及行号和列号。

A very simple example 一个非常简单的例子

function example1(){
  var range = SpreadsheetApp.getActiveRange();
  range.setBackgroundColor('red');
}

Recently the Class RangeList was introduced so now is possible to speedup the algorithms to edit non-adjacent cells. 最近引入了Class RangeList,因此现在可以加快算法来编辑不相邻的单元格。

function example2() {
  var sheet = SpreadsheetApp.getActiveSheet()
  var redList = [];
  var vls = sheet.getRange('A1:A' + sheet.getLastRow()).getValues();
  for(var i = 0; i < vls.length; i++){
    if(vls[i][0]=== 'red') redList.push('A'+ (i + 1));
  }
  var redRangeList = sheet.getRangeList(redList).setBackground('red');
}

Please note that the for loop doesn't make calls to the Google Apps Script classes and methods, it use pure JavaScript which make it faster than other alternatives. 请注意,for循环不会调用Google Apps脚本类和方法,它使用纯JavaScript使其比其他方法更快。 Then with a single call the color of several non-adjacent cells is set. 然后,通过一次呼叫,设置了几个不相邻单元格的颜色。

You might like to try it this way: 您可能想这样尝试:

  function foo(){
  var ss=SpreadsheetApp.openById('1oh5wXFi4YM1bDvfF27pu1qHDKQEhn02drtfsrRgxArQ');
  //var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('General');
  //var sh=ss.getActiveSheet();
  var range=sh.getRange(1,4,sh.getLastRow(),1);
  var values=range.getValues();
  for(var i=0;i<values.length;i++){
    if(values[i][0].match(/Rensselaer|Albany|Saratoga/)){
      sh.getRange(i+1,4).setBackgroundColor("#99CC99");
    } 
  }
}

This works too: 这也适用:

function foo(){
  var ss=SpreadsheetApp.openById('1oh5wXFi4YM1bDvfF27pu1qHDKQEhn02drtfsrRgxArQ');
  //var ss=SpreadsheetApp.getActive();
  var sh=ss.getSheetByName('General');
  //var sh=ss.getActiveSheet();
  var range=sh.getRange(1,4,sh.getLastRow(),1);//gets the smallest range that has data in it
  var values=range.getValues();
  var colors=range.getBackgrounds();//Creates the correct size area with the starting colors
  for(var i=0;i<values.length;i++){
    if(values[i][0].match(/Rensselaer|Albany|Saratoga/)){//you can add more alternatives easily
      colors[i][0]='#99cc99';
      //sh.getRange(i+1,4).setBackgroundColor("#99CC99");
    } 
  }
  range.setBackgrounds(colors);
}

Have questions? 有问题吗?

Try reading the Google Apps Script Documentation . 尝试阅读Google Apps脚本文档

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM