Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

324
Visualizações
How to implement IF-ELSE statement to change the cell value using app script?

In here I am trying to change the cell value based on the IF-ELSE statement.

As you can see in the picture below, when user try to run the script for the first time, few files from google drive will be listed in the sheet with the STATUS mark as "new file" (row 2 till 5).

However, if there is any new files been uploaded into the google drive, user will run a script "check new files" to populate only the uploaded new files into the google sheet (row 6 and 7). But their STATUS mark is blank as shown in the picture.

enter image description here

Therefore i used the IF-ELSE statement, to convert the STATUS of row 2 till 5 (""new file" into "old file"") AND the blank row of 6 and 7 as "new file".

Here is the code:

function updateMarker(){
var sheet = 
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");

var cellValue = sheet.getRange("C2:C"+ sheet.getLastRow()).getValue(); 
Logger.log(cellValue);

if (cellValue == 'new file'){
sheet.getRange("C2:C"+ sheet.getLastRow()).setValue('OLD FILE');
 }
if(cellValue == '' ){
sheet.getRange("C2:C"+ sheet.getLastRow()).setValue('NEW FILE');
 }
  }

If i run the code, it converts the whole cell value from row 2 till 7 as "old file". Which i guess it detects only the first if statement if (cellValue == 'new file').

My goal is

  1. to convert the "new file" marker into "old file"
  2. to set the empty cell value of the new uploaded files as "new file"

How can i solve this problem?

AFTER IMPLEMENTING THE SOLUTION CODE GIVEN enter image description here

enter image description here

After i run for 2nd and 3rd time the STATUS changes vice versa.

Isn't the STATUS changes only if the cell value is "new file" and if there is empty cell ?

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Calling getValue() on a range of more than one cell will only return the value in the upper-left most cell in the range.

So the line

var cellValue = sheet.getRange("C2:C"+ sheet.getLastRow()).getValue()

actually just sets cellValue to the value in C2, or 'new file'.

This makes the line:

if (cellValue == 'new file')

always evaluate to true, so all cells are changed.

Modifications:

  • Use .getValues() instead of getValue()
  • Loop through all the cells
  • Set the new OLD FILE/NEW FILE array back to the column range
function updateMarker() {
  const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1")

  const cells = sheet.getRange("C2:C" + sheet.getLastRow())
  const cellValues = cells.getValues()
  Logger.log(cellValues)

  const newValues = []

  cellValues.forEach((row) => {
    if (row[0] === "new file") {
      newValues.push(['OLD FILE'])
    }
    else if (row[0] === "OLD FILE" || row[0] === "NEW FILE") {
      newValues.push([row[0]])
    }
    else {
      newValues.push(['NEW FILE'])
    }
  })
  
  cells.setValues(newValues)
}
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda