简体   繁体   中英

Add text to a cell automatically if another cell is not empty

I've managed to use scripts to automate a few things on a Google Sheet I'm working on, but can't quite seem to work this one out.

I want to insert the letter Y into Column B if Column R isn't empty .

I tried to follow this one but got a bit confused - If cell contains a text then add text automatically to another column cell

I couldn't figure out how to specify the target column (R) or what to put where ???? is to make it 'not empty'

function fillColB() {
  var s = SpreadsheetApp.getActiveSheet();
  var data = s.getDataRange().getValues();
  var data_len = data.length;
  for(var i=0; i<data_len; i++) {
    if(data[i][0] == "?????") {
      s.getRange(i+1,2).setValue("Y");
      }
    }
  }

Can anyone suggest a script I could add to make this happen?

If you want to get only the data from column R, you could use

var data = s.getRange("R1:R").getValues();

Then to loop over that just like how you're doing.

Testing if the length is 0 should be equivalent to testing if the cell is blank:

if(data[i][0].length == 0) {

Although a cell set to an empty string ="" would also have length zero, meaning this script would act like it's a blank cell.

Final code:

function fillColB() {
var s = SpreadsheetApp.getActiveSheet();
var data = s.getRange("R1:R").getValues();
var data_len = data.length;
for(var i=0; i<data_len; i++) {
  if(data[i][0].length == 0) {
    s.getRange(i+1,2).setValue("Y");
  } else {
    s.getRange(i+1,2).setValue("N");
  }
}

}

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