Google Slidesで、現在選択している表の特定セル内にある指定文字を置換しようと試したコードです。
I tried to replace a specified character within a specific cell of the currently selected table in Google Slides.
今回の例では、セルの中に含まれる 「 Google」 というテキストを空文字に置換します。
この例では「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()