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

179
Visualizações
Regex to target different cells based on what it starts with and only replacing the first character IF it matches THEN - Google App Script

So I have a column in my spreadsheet that looks like the below image. shows values in column.

My ultimate goal is to create a script that removes the first symbol IF applicable else just get the value.

So far I was able to remove all the symbols in a cell but that's not my actual goal the code I've been using is the below.

  function removePlus() {
  var sheet = SpreadsheetApp.getActiveSheet();
  var range = sheet.getRange('BK2:BK');
  var textFinder = range.createTextFinder("+").matchFormulaText(true)
  textFinder.replaceAllWith("");

I posted a question yesterday and was directed to look at regex. However, it appears with regex I have to put in the string of values I want to replace, but as you can see the values in the column vary. So I'm really not sure how to tackle this in a short time-frame. I have been reading up on resources but to no prevail. If possible detailed explanations or pointing me in the right direction will be helpful. I'm not looking for anyone to just tell me code, I really do want to learn on my own but I'm just not having any success. Thank you for the time.

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

0

Something like this?

function removeFirstSymbol() {

  const sheet = SpreadsheetApp.getActiveSheet()
  const range = sheet.getRange(`BK2:BK`)
  const values = range.getDisplayValues().flat()

  const formatted = values.map(row => {
    return (new RegExp(`[^A-Za-z0-9]`).test(row.charAt(0))) 
    ? [row.slice(1)]
    : [row]
  })

  range.setValues(formatted)

}

Commented:

  // Get all values as they're displayed...
  const values = range.getDisplayValues().flat()

  // For each value...
  const formatted = values.map(row => {
    // If the first character is not a number or letter...
    return (new RegExp(`[^A-Za-z0-9]`).test(row.charAt(0)))
    // Remove the first letter,
    ? [row.slice(1)]
    // Or keep as is.
    : [row]
  })

Note, this removes ALL symbols in the first character position, as I had looked at your previous post.

about 4 years ago · Juan Pablo Isaza Relatório

0

  • Use ^[+*&%=]
    • ^ Start of string
    • [] Character class: Any of the characters inside []:+,*,&,% and =
  • Also use useRegularExpressions to use regex parsing
range.createTextFinder("^[+*&%=]")
    .matchFormulaText(true)
    .useRegularExpressions(true)
    .replaceAllWith("")
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