Skip to content

Commit 6f6fa90

Browse files
committed
get_id_from_url
Signed-off-by: Alex Ivanov <ai@contributor.pw>
1 parent ba1828a commit 6f6fa90

File tree

6 files changed

+114
-0
lines changed

6 files changed

+114
-0
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"timeZone": "Europe/Moscow",
3+
"dependencies": {},
4+
"exceptionLogging": "STACKDRIVER",
5+
"runtimeVersion": "V8"
6+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"type": "standalone"
3+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* Gets ID from URL
3+
*
4+
* @param {string} url The file url
5+
* @returns {string} The file id
6+
*/
7+
function getIdFromUrl(url) {
8+
const match = url.match(/[\w-_]{15,}/);
9+
return match ? match[0] : undefined;
10+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: 'Get file/folder ID from url'
3+
date: '2021-03-07'
4+
description: 'Shows how to get file ID from url'
5+
tags: ['js', 'common']
6+
categories: ['snippets']
7+
---
8+
9+
{{< toc >}}
10+
11+
## Get file/folder ID from url
12+
13+
### Snippet
14+
15+
- {{< externalLink >}}
16+
- {{< commentLink >}}
17+
18+
{{< codeFromFile "index.js" >}}
19+
20+
### Run it
21+
22+
{{< codeFromFile "run.js" >}}
23+
24+
### Test
25+
26+
> Info! You need enable `Drive` advanced service manually
27+
28+
{{< codeFromFile "test.js" >}}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* This code runs the snippet
3+
*/
4+
function run() {
5+
let url =
6+
'https://docs.google.com/spreadsheets/d/19GRd3EarV_SM3Y8MYdpWvQNYZVZgLZih4EO4gX_JSko/edit#gid=1702180728';
7+
console.log(getIdFromUrl(url));
8+
url =
9+
'https://drive.google.com/drive/folders/1nUB4UDrCNSbNmjSXy2H5DNz09s1LDEIb';
10+
console.log(getIdFromUrl(url));
11+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
/**
2+
*
3+
*/
4+
function runTest() {
5+
let passed = true;
6+
const maxItems = 6000;
7+
const items = [];
8+
9+
let fileList;
10+
do {
11+
fileList = Drive.Files.list({
12+
maxResults: 1000,
13+
pageToken: fileList ? fileList.nextPageToken : '',
14+
});
15+
items.push(...fileList.items);
16+
console.info(items.length, fileList.nextPageToken);
17+
} while (fileList.nextPageToken && items.length < maxItems);
18+
19+
items
20+
.reduce(
21+
(p, item) => (
22+
p.push(
23+
...['alternateLink', 'downloadUrl', 'exportLinks'].reduce(
24+
(r, key) => (
25+
typeof item[key] === 'string'
26+
? r.push({
27+
url: item[key],
28+
id: item.id,
29+
})
30+
: typeof item[key] === 'object'
31+
? r.push(
32+
...Object.keys(item[key]).map((elk) => ({
33+
url: item[key][elk],
34+
id: item.id,
35+
}))
36+
)
37+
: '',
38+
r
39+
),
40+
[]
41+
)
42+
),
43+
p
44+
),
45+
[]
46+
)
47+
.forEach((entry, i) => {
48+
const test = getIdFromUrl(entry.url) === entry.id;
49+
if (test == undefined) {
50+
passed = false;
51+
console.warn(i, entry.url, entry.id, getIdFromUrl(entry.url));
52+
}
53+
});
54+
55+
if (passed) console.log('Test is passed', items.length);
56+
}

0 commit comments

Comments
 (0)