craftApprentice
craftApprentice

Reputation: 2777

Google apps script how to parse a link and extract the document ID?

Given a document link, is there a simple method which will allow me to extract the ID from a Google document?

Or the only way is write a script that do something like this:

var linkString = "https://docs.google.com/spreadsheet/ccc? key=0AmEr9uNtZwnNdFNkNklYc3pVUzZINUV4eUtWVWFSVEf&usp=drive_web#gid=1"
var docID = '';

for (i=0; i<=linkString.length(); i++) {
    if (linkString[i] = '='){        
        while (linkString[i] !== '&') {
            docID =+ linkString[i];
            i+=1 ;
       }
    return docID
    }    
}

Upvotes: 2

Views: 2143

Answers (1)

Serge insas
Serge insas

Reputation: 46792

You could make it simple using the split method

  var url = "https://docs.google.com/spreadsheet/ccc? key=0AmEr9uNtZwnNdFNkNklYc3pVUzZINUV4eUtWVWFSVEf&usp=drive_web#gid=1"
  var id = url.split('key=')[1].split('&')[0];
  Logger.log(id)

Upvotes: 3

Related Questions