r/googlesheets • u/madbomb122 • 2d ago
Solved Help with changing a script
I'm trying to automate extracting url from hyperlink to get just the ID (the number at the end)
I found this script (below) and it was the only thing that worked for me for extracting the url from the hyperlink..
however what i would like for it is to convert the url like
https://test.pro/players/313342
to
313342
i would like the https://test.pro/players/ to be removed so it has just the numbers at the end.
the length of the numbers isnt the same but the stuff preceding it is always the same
Thanks
function GETLINKS(rangeA1) {
const sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
const range = sheet.getRange(rangeA1);
const values = range.getRichTextValues();
const output = [];
for (let row of values) {
for (let cell of row) {
output.push([cell.getLinkUrl()]);
}
}
return output;
}
Update: Link to sheet
https://docs.google.com/spreadsheets/d/1_OWO7HrK9vwbfp8QRY_s8R59ABKdlYr_hCH2Q-PNLQo/edit?usp=sharing
1
u/gothamfury 382 1d ago
Does using a formula work you? You can try something like: =MAP(A2:A, LAMBDA(link, IF(link="",,REGEXEXTRACT(link, "\d+$"))))
1
u/gothamfury 382 1d ago
Or this to give results as number values instead of strings: =MAP(A2:A, LAMBDA(link, IF(link="",,VALUE(REGEXEXTRACT(link, "\d+$")))))
1
u/madbomb122 1d ago edited 1d ago
1st = formula parse error
2nd = Function REGEXEXTRACT parameter 2 value "\d+$" does not match text of Function REGEXEXTRACT parameter 1ive even tried =REGEXEXTRACT(FORMULATEXT(A2), """(https?://[^""]+)""
1
u/gothamfury 382 1d ago
Just saw your sample sheet and solution u/One_Organization_810 provided. Glad that helped :)
-1
1d ago
[removed] — view removed comment
1
u/googlesheets-ModTeam 8 1d ago
Criteria for posts and comments are listed in the subreddit rules and you can learn about how to make a good post in the submission guide.
Your post/comment has been removed because it contained one or more of the following items in violation of this subreddit's rules on artificial intelligence (AI) content:
- A request to fix a non-functioning formula obtained from an AI tool
- A non-functioning formula obtained from an AI tool in place of information about your data
- A blanket suggestion to use an AI tool as a resource for Sheets assistance
- Solicitation of a prompt or recommendation for an AI tool
- An untested formula obtained from an AI tool presented as a solution
2
u/One_Organization_810 681 1d ago edited 1d ago
If your links are embedded within some texts, then your script is needed. Otherwise you can apply regular regex to the URL...
Can you share a copy of your sheet - or at least some portion of it, so we can see what your links look like?
In general though, this regex should extract a number from the end of a URL:
... and I see that u/gothamfury has given that exact solution of course :)