Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

190
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!