简体   繁体   English

在范围上应用If / Then(google-apps-script)

[英]Apply If/Then on a Range (google-apps-script)

I'm slowly working my way towards understanding how to formulate a common programming process, but I'm in need of someone who is willing to further teach me how to fish (in this JS pond). 我正在慢慢地努力理解如何制定一个通用的编程过程,但是我需要一个愿意进一步教我如何钓鱼的人(在这个JS池塘中)。

I want to test a range of cells (ex: A1:A10) and if there is a "," found within a cell in that range, I want to write a formula in the cell 5 columns to the right of that cell. 我想测试一个单元格范围(例如:A1:A10),如果该范围内的某个单元格内有一个“,”,我想在该单元格右侧的5列单元格中编写一个公式。

So far I have been able to get it to work when specifying specific cells, but I'm not sure how to adopt this to handle a range of cells. 到目前为止,我已经能够在指定特定单元格时使它工作,但是我不确定如何采用它来处理一系列单元格。

(btw, this is something that I understand how to do in VBA - the absence of the SPLIT function notwithstanding - but this language is obviously a very different animal) (顺便说一句,这是我了解的在VBA中的操作-尽管没有SPLIT函数-但这种语言显然是一种非常不同的动物)

Here is what I have now: 这是我现在所拥有的:

var defSheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")   
var test1 = defSheet1.getRange("A1").getValue();
var splitCell = '=SPLIT(A1,",")';
if (test1.indexOf(",") !== -1)  
{
defSheet1.getRange("F1").setFormula(splitCell);
}
 else 
  { 
Browser.msgBox("NO SPLIT NECESSARY");   
  } 

(btw, the actual range is going to be determined by using getDataRange, but for simplicity sake, I'm using a pre-established range here) (顺便说一句,实际范围将通过使用getDataRange确定,但为简单起见,我在这里使用了预先建立的范围)

I've already learned quite a bit here, and I'm gradually gaining the ability to "think" in JS, but a VBA " For x = 1 to numLastRow " concept isn't clicking for me in JS. 我已经在这里学到了很多东西,并且逐渐获得了在JS中“思考”的能力,但是在JS中,VBA的“ 对于x = 1到numLastRow ”的概念并不是我想要的

When you're working on multiple rows and/or columns from spreadsheets, one challenge is converting between indexes and the various ways that cell locations can be expressed. 当您处理电子表格中的多行和/或多列时,一项挑战是在索引和表示单元格位置的各种方式之间进行转换。

  • In A1Notation, columns are expressed first, as a letter, followed by row, as a number starting at 1. 在A1Notation中,列首先以字母表示,然后以行表示,从1开始。
  • R1C1 notation flips the order of rows and columns, and both dimensions are numbers starting from 1. R1C1表示法翻转行和列的顺序,并且两个维都是从1开始的数字。
  • Arrays in javascript start from index 0. As you evolve your script to use getDataRange().getValues() , this will be important. JavaScript中的数组从索引0开始。随着脚本的发展以使用getDataRange().getValues() ,这将很重要。 In the two-dimensional array returned by getValues() , rows are expressed first, then column, as in data[row][column] . getValues()返回的二维数组中,首先表示行,然后表示列,如data[row][column] The upper bound of your loops will need to be adjusted accordingly, and you may need to +1 if you're referencing Range methods like setFormula() . 循环的上限需要相应地进行调整,如果要引用诸如setFormula()类的Range方法,则可能需要+1

The code snippet below will do what you're looking for, as a starting point. 下面的代码段将作为您寻找的起点。 I've left out the else block to reduce noise. 我省略了else块以减少噪音。 Note the use of getA1Notation() to build the SPLIT formula dynamically - a straight-forward evolution of your script. 注意,使用getA1Notation()动态生成SPLIT公式-脚本的直接演变。

You'll need to define numLastRow yourself, and since the script is accessing Range methods throughout, 10 would mean cell A10 . 您需要自己定义numLastRow ,并且由于脚本始终在访问Range方法,因此10表示单元格A10

var defSheet1 = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")

for (var x=1; x<=numLastRow; x++) {
  var srcRange = defSheet1.getRange(x,1);
  var value = srcRange.getValue();  // Get value at Row x, Column 1
  if (value.indexOf(",") !== -1) {
    var splitCell = '=SPLIT(' + srcRange.getA1Notation() + ',",")';
    defSheet1.getRange(x,5).setFormula(splitCell);
  }
}; 

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

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