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

2024年6月5日水曜日

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


以下のリンク先では、文字を指定せずにセル内のすべての値を太字にしました。
In the following link, all the text within the cell was bolded without specifying any particular text.


今回は、現在選択している表の特定セル内にある指定文字を太字にしようと試したコードです。
In this example, I tried to use code to bold specified text within a specific cell of the currently selected table.


今回の例では、セルの中に含まれる 「Google」 というテキストを太字にします。
In this example, the text " Google" contained within a cell is replaced with an empty string.


今回太字にする文字を含むテキスト
The text containing the words to be bolded this time

Trying to use Google Apps Script to bold a specific value within a targeted cell in a table in Google Slides.




手動でやる場合は上部の「B」アイコンをクリックすると太字になります。
Manually, you can click the "B" icon at the top to make the text bold.


⌘キーを押しながらテキストを選択すると、複数のテキストを選択できます。
You can select multiple text elements by holding down the ⌘ (Command) key while selecting.





Apps Script

Code.gs
function boldSelectedText() {
const value = "Google";
const table = getSelectedTable();
const row = 1;
const col = 1;
const cell = table.getCell(row, col);

const textRange = cell.getText();
const text = textRange.asString();
const indices = [];
let startIndex = text.indexOf(value);
while (startIndex !== -1) {
indices.push(startIndex);
startIndex = text.indexOf(value, startIndex + value.length);
}

for (let i = 0; i < indices.length; i++) {
const startIndex = indices[i];
const endIndex = startIndex + value.length;
const textStyle = textRange.getRange(startIndex, endIndex).getTextStyle();
textStyle.setBold(true);
}
}


今回の例では、スライドに3行2列の表を用意して、2行目2列のセルに「 Google Apps Script」と「 Google Slides」というテキストを含めておきます。
In this example, I prepare a 3-row, 2-column table on the slide and include the text "Google Apps Script" and "Google Slides" in the cell at row 2, column 2.


表を選択して、Code.gsのboldSelectedText()を実行すると、「Google」のテキストが太字になります。
After selecting the table and run replaceValue() in Code.gs, the text "Google" will be bolded.


Reference

Class TextStyle > setBold(bold) 

Google Slidesの表内の文字を太字にしたい - Bold the 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 に課題が上がっていることが...