简体   繁体   中英

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).

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.

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)

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)

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.

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.
  • R1C1 notation flips the order of rows and columns, and both dimensions are numbers starting from 1.
  • Arrays in javascript start from index 0. As you evolve your script to use getDataRange().getValues() , this will be important. In the two-dimensional array returned by getValues() , rows are expressed first, then column, as in 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() .

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. Note the use of getA1Notation() to build the SPLIT formula dynamically - a straight-forward evolution of your script.

You'll need to define numLastRow yourself, and since the script is accessing Range methods throughout, 10 would mean cell 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);
  }
}; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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