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

2024年6月3日月曜日

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


Google Slidesで、現在選択している表の特定セル内にある指定文字を置換しようと試したコードです。
I tried to replace a specified character within a specific cell of the currently selected table in Google Slides.


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

この例では「Google」の前にある半角スペースも削除しています。
In this example, I'm also removing the half-width space before the text "Google".

今回置換するテキスト

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



Code.gs
function replaceValue() {
const value = " Google";
const table = getSelectedTable();
const row = 1;
const col = 1;
const regExp = new RegExp(value, "g");
const cell = table.getCell(row, col);
const textRange = cell.getText();
const originalText = textRange.asString();
const text = originalText.replace(regExp, "");
cell.getText().setText(text);
}

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


今回の例では、スライドに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のreplaceValue()を実行すると、「 Google」のテキストが空文字に置換されます。
After selecting a table and executing the replaceValue() function in Code.gs, the text " Google" will be replaced with an empty string.


Reference

Google Slidesで現在選択している表に値を入れたい - Insert values into the active table

String.prototype.replace()

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 に課題が上がっていることが...