Copy URL to a column according to a criteria

Asked

Viewed 36 times

1

I am trying with the following code to take the URL of an entire row and store it in a "J" column if another "E" column contains some value, that is, it is not empty. The code executes, however, copies the URL ignoring whether the "E" column is empty or not. That is, whether this column has value or not, the URL is generated, as shown Where I am missing?

function geraURL() {
  var spreadsheet = SpreadsheetApp.getActive();


  for (var i=2; i<50;i++) {
    if (spreadsheet.getRange('E'+i).activate()!= 0) {
        spreadsheet.getRange('J'+i).activate();
        spreadsheet.getCurrentCell().setValue('https://docs.google.com/spreadsheets/d/11LnRyO150lEMkDB-R7AoDHniP4sjUt4PiuLiH4/edit#gid=11393109881&range='+i+':'+i);
        spreadsheet.getRange('J'+i).activate();
    } else {
        spreadsheet.getCurrentCell().setValue('');
    }
  }
}

inserir a descrição da imagem aqui

1 answer

1


You need to check that the cell value is empty through the method getValue, for the activate returns an object.

function geraURL() {
  var spreadsheet = SpreadsheetApp.getActive();


  for (var i=2; i<50;i++) {
    if (spreadsheet.getRange('E'+i).getValue()) {
        spreadsheet.getRange('J'+i).activate();
        spreadsheet.getCurrentCell().setValue('https://docs.google.com/spreadsheets/d/11LnRyO150lEMkDB-R7AoDHniP4sjUt4PiuLiH4/edit#gid=11393109881&range='+i+':'+i);
    }
  }
}
  • It worked. Thank you.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.