Apps Script公式リファレンス: Apps Script Reference |障害・課題追跡: IssueTracker |Google Workspace: Status Dashboard - Summary

2024年6月6日木曜日

Google Slidesの表内の指定文字を置換して太字にしたい - Replace and bold specific text within a table in Google Slides


今回は、以下のリンク先のコードを組み合わせてみたものです。
In this example, I combined the code from the following link.


Google Slidesで現在選択している表内の特定のセル内にある指定した文字を置換して太字にしてみます。
I will try to replace and bold specific text within a specific cell of the currently selected table in Google Slides.



Procedures

3行2列の表を用意して、2行目2列目にテキストを入力しておきます。
Prepare a table with 3 rows and 2 columns, and enter text in the second row and second column.


その際、太字にしたい単語を [ ] で囲んでおきます。
Enclose the word you want to bold in square brackets [].


今回の例で使うテキスト
The text used in this example.

Trying to replace and bold a specific text within a table in Google Slides using Google Apps Script.


今回の例では、replace と bold を [] で囲んでいます。
In this example, "replace" and "bold" are enclosed in square brackets [].


表を選択して replaceBoldTargetWords() を実行すると、replace と bold の [] が空文字に置き換わり、太字になります。
After selecting the table and executing replaceBoldTargetWords(), the square brackets [] are removed, and the text "replace" and "bold" are made bold.



Apps Script

Code.gs
function replaceBoldTargetWords() {
const table = getSelectedTable();
const row = 1;
const col = 1;
const cell = table.getCell(row, col);

const textRange = cell.getText();
let text = textRange.asString();
let values = text.match(/(?<=\[).+?(?=\])/g);

text = text.replace(/\[|\]/g, '');
cell.getText().setText(text);
for (let i = 0; i < values.length; i++) {
const value = values[i];
const startIndex = text.indexOf(value);
const endIndex = startIndex + value.length;
textRange.getRange(startIndex, endIndex).getTextStyle().setBold(true);
}
}

function getSelectedTable() {
const presentation = SlidesApp.getActivePresentation();
const selection = presentation.getSelection();
const pageElement = selection.getPageElementRange().getPageElements()[0];
const table = pageElement.asTable();
return table;
}


Tips


最初は単語を太字にしてから [ ] を空文字にしようとしましたが、それだと [] が消えるだけで単語が太字になりませんでした。
At first, I tried to make the words bold and then replace the square brackets [] with empty strings, but that only removed the brackets and did not bold the words.


cell.getText().setText(text);
置換する際の上記コードがテキスト全体を上書きするため、以下の手順で試したのが今回書いたコードです。
Since the above code overwrites the entire text when replacing, I tried the following steps, which led to the code I wrote this time.


STEPS
1. [ ] を目印にして太字にしたい単語を取得する
Retrieve the words to be bolded using the square brackets [] as markers.

let values = text.match(/(?<=\[).+?(?=\])/g);



2. 全テキストを取得して[ ] を空文字に置換する
Replace all square brackets [] with empty strings after retrieving the entire text.

text = text.replace(/\[|\]/g, '');



3. 取得しておいた単語を太字にする
Bold the retrieved words

textRange.getRange(startIndex, endIndex).getTextStyle().setBold(true);



注意点として、上記のコードでは取得した単語の前方に同一の単語がある場合、前方の単語が太字になります。
Please note that with the above code, if the same word appears before the retrieved word, the previous word will be bolded.


今回やりたかったことのスコープ外なので、この記事ではこの挙動は許容しています。
Since this behavior is outside the scope of what I wanted to achieve this time, I am allowing it in this article.


Reference

Google Slidesの表内の指定文字を太字にしたい - Bold specific text within a table in Google Slides

Google Slidesの表のテキストを置換したい - Replace text in a table in Google Slides



Latest post

Google Apps Scriptの障害時はIssueTrackerを見てみる - Incidents for Apps Script are reported on Issue Tracker

IssueTracker > Apps Script issues https://issuetracker.google.com/savedsearches/566234 Google Apps Scriptの障害時は IssueTracker に課題が上がっていることが...