Last active
May 13, 2022 06:39
-
-
Save ttsukagoshi/24ff40db41abd5ed9062addbb8572742 to your computer and use it in GitHub Desktop.
Googleスプレッドシートで、選択したセル範囲の文字列をテキストリンクに変換するためのGoogle Apps Script(GAS)。使用したいスプレッドシートのメニュー>拡張機能>Apps Scriptからエディタを表示して、convert2hyperlink.jsの内容をコピペ&保存する。保存後、スプレッドシートを再読み込みすると、メニューに「URL変換」という項目が追加されているので、選択、実行。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Copyright 2022 Taro TSUKAGOSHI | |
* | |
* Licensed under the Apache License, Version 2.0 (the "License"); | |
* you may not use this file except in compliance with the License. | |
* You may obtain a copy of the License at | |
* | |
* http://www.apache.org/licenses/LICENSE-2.0 | |
* | |
* Unless required by applicable law or agreed to in writing, software | |
* distributed under the License is distributed on an "AS IS" BASIS, | |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
* See the License for the specific language governing permissions and | |
* limitations under the License. | |
* | |
* See the GitHub Gist for more details: https://gist.github.com/ttsukagoshi/24ff40db41abd5ed9062addbb8572742 | |
*/ | |
function onOpen() { | |
SpreadsheetApp.getUi() | |
.createMenu('URL変換') | |
.addItem('ハイパーリンクに変換', 'convert2Hyperlink') | |
.addToUi(); | |
} | |
function convert2Hyperlink() { | |
const ui = SpreadsheetApp.getUi(); | |
try { | |
// 実行確認 | |
const alertConfirmation = ui.alert( | |
'選択された範囲にあるURLをハイパーリンクに変換します。実行しますか?', | |
ui.ButtonSet.YES_NO | |
); | |
if (alertConfirmation !== ui.Button.YES) { | |
// プロンプトでのボタンが「はい(Yes)」以外だった場合 | |
// =「いいえ(No)」ボタン押下 or 右上の×でプロンプト終了 | |
throw new Error('キャンセルされました。'); | |
} | |
const selectedRangeList = SpreadsheetApp.getActiveSpreadsheet().getActiveRangeList(); | |
if (!selectedRangeList) { | |
throw new Error('範囲が選択されていません。1つ以上のセルを選択してください。'); | |
} | |
selectedRangeList.getRanges().forEach(targetRange => { | |
const richTextValues = targetRange | |
.getValues() | |
.map(row => row | |
.map(cell => SpreadsheetApp | |
.newRichTextValue() | |
.setText(cell) | |
.setLinkUrl(cell) | |
.build())); | |
targetRange.setRichTextValues(richTextValues); | |
}); | |
} catch (error) { | |
console.error(error.stack); | |
ui.alert(error.message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
こちらからサンプルスプレッドシートをコピーできます→ [Sample] Convert to Hyperlink - Google スプレッドシート