Skip to content

Commit 62ff0a8

Browse files
committed
unlink urls
Signed-off-by: Alex Ivanov <ai@contributor.pw>
1 parent 5eb0a89 commit 62ff0a8

File tree

6 files changed

+81
-0
lines changed

6 files changed

+81
-0
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"timeZone": "Europe/Moscow",
3+
"exceptionLogging": "STACKDRIVER",
4+
"runtimeVersion": "V8"
5+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"type": "container-bound-sheet",
3+
"src": []
4+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* Removes all links from the range
3+
*
4+
* @param {GoogleAppsScript.Spreadsheet.Range} range A range
5+
* @return {GoogleAppsScript.Spreadsheet.RichTextValue[][]}
6+
*/
7+
function unlinkUrls_(range = SpreadsheetApp.getActiveRange()) {
8+
return range.setRichTextValues(
9+
range.getRichTextValues().map((rowRichTextValues) =>
10+
rowRichTextValues.map((richTextValue) => {
11+
let copy;
12+
if (richTextValue.getRuns().some((ruin) => ruin.getLinkUrl())) {
13+
copy = richTextValue.copy();
14+
copy.setLinkUrl(undefined);
15+
return copy.build();
16+
}
17+
return richTextValue;
18+
})
19+
)
20+
);
21+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
---
2+
title: 'Unlink all URLs in the range'
3+
date: '2021-04-12'
4+
description: 'Using the RichTextValues for clearing a specific range'
5+
tags: ['sheets', 'Sheets advanced service']
6+
categories: ['snippets']
7+
images: ['./snippets/sheets/unlink_urls/screenrecord.gif']
8+
---
9+
10+
{{< toc >}}
11+
12+
## Unlink all URLs in the range
13+
14+
![Snippet of Unlink all URLs in the range](./screenrecord.gif)
15+
16+
### Snippet
17+
18+
- {{< externalLink >}}
19+
- {{< commentLink >}}
20+
- {{< scrvizLink >}}
21+
22+
{{< codeFromFile "index.js" >}}
23+
24+
### Run it
25+
26+
{{< codeFromFile "run.js" >}}
27+
28+
### Issues
29+
30+
This code does not remove links if they are the only content in the cell. See [google apps script - Unlink all hyperlinks from a sheet - Stack Overflow](https://stackoverflow.com/questions/62271646/unlink-all-hyperlinks-from-a-sheet/62275201#62275201)

snippets/sheets/unlink_urls/run.js

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/* global unlinkUrls_ */
2+
3+
/**
4+
* Runs the snippet.
5+
* Unlink URLs
6+
*/
7+
function run() {
8+
const sheet = SpreadsheetApp.getActiveSheet();
9+
const range = sheet.getDataRange();
10+
unlinkUrls_(range);
11+
}
12+
13+
/**
14+
* Create menu for handy use
15+
*/
16+
function onOpen() {
17+
SpreadsheetApp.getUi()
18+
.createMenu('Apps Script Snippets')
19+
.addItem('Unlink all urls on the Sheet', 'run')
20+
.addToUi();
21+
}
785 KB
Loading

0 commit comments

Comments
 (0)