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

191
Visualizações
Use JavaScript to get Google Sheet ID from URL

I'm currently using an input that parses a Google Sheet ID and inserts into code.

let sheetIdInput = document.getElementById('sheetidinput');

let id = sheetIdInput.value;

I want users to be able to paste the entire Google Sheet URL into this input sheetidinput and it to only use the Sheet ID from the URL.

Example Google Sheet URL is

https://docs.google.com/spreadsheets/d/132SaKuLRdHcwEKhhQVOKd2_3j30nFvQbTgplh4nPZOg/edit#gid=0

The Sheet ID is between .../d/ and /edit... in this case 132SaKuLRdHcwEKhhQVOKd2_3j30nFvQbTgplh4nPZOg

This pattern repeats itself in all Google Sheet URLS. Any help would be greatly appreciated.

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

0

You could use regex to do it :

let url = "https://docs.google.com/spreadsheets/d/132SaKuLRdHcwEKhhQVOKd2_3j30nFvQbTgplh4nPZOg/edit#gid=0";
let capturedId = url.match(/\/d\/(.+)\//)

console.log(capturedId[1])
// prints : 132SaKuLRdHcwEKhhQVOKd2_3j30nFvQbTgplh4nPZOg 
about 4 years ago · Juan Pablo Isaza Relatório

0

You can use a regex:

var re = /.*\/d\/(.*)\/edit.*/;
var url = "https://docs.google.com/spreadsheets/d/132SaKuLRdHcwEKhhQVOKd2_3j30nFvQbTgplh4nPZOg/edit#gid=0
";
var match = url.match(re);
var id = match[1]
//Use id
}
about 4 years ago · Juan Pablo Isaza Relatório

0

Even though the above solutions both work well for your situation, I am adding this one as well:

let url = "https://docs.google.com/spreadsheets/d/132SaKuLRdHcwEKhhQVOKd2_3j30nFvQbTgplh4nPZOg/edit#gid=0";
let matches = /\/([\w-_]{18,})\/(.*?gid=(\d+))?/.exec(url);
console.log("spreadsheet id " + matches[1]);
console.log("sheet id " + matches[3]);

The matches makes use of regex in the following way:

  • the first capturing group is used to return the id of the spreadsheet by matching any word character and also assuming that the id has more than 18 characters;

  • the second and third capturing group is used to return the id of the sheet by matching the characters after gid.

Reference

  • Regular expressions.
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