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

2024年5月27日月曜日

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


以下リンク先でもスライド内の表に値を入れるコードを書きました。
I also wrote articles on how to insert values into a table in Google Slides using Apps Script. You can find them at the following links.


今回は上記を組み合わせました。
This time, I combined the approaches from the above.




コードを実行する前に、4行4列の表を配置して選択しておきます。
Before running the code, place and select a 4-row, 4-column table on the slide.

一行目は見出し行にしておきます。
Set the first row as the header.



Code.gs
function setValuesToSlideTable() {
var text = `get: 得る: v.: To obtain or receive something.
take: 取る: v.: To carry or move something from one place to another.
make: 作る: v.: To create or produce something.
give: 与える: v.: To offer or hand over something to someone.`;
var values = text.split("\n");
var arrays = []
for(var i = 0; i < values.length; i++) {
var row = values[i];
var cols = row.split(": ");
var array = [cols[0], cols[1], cols[2], cols[3]];
arrays.push(array);
}
insertValuesIntoSelectedTable(arrays);
}

function insertValuesIntoSelectedTable(values) {
var presentation = SlidesApp.getActivePresentation();
var selection = presentation.getSelection();
var pageElement = selection.getPageElementRange().getPageElements()[0];
var table = pageElement.asTable();
var tableRows = table.getNumRows()-1;// Subtract 1 since the first row of the table is a header.

if(values.length !== tableRows) {
var diffRows = values.length - tableRows;
if(0 < diffRows) {
addRows(table, diffRows);
} else {
deleteRows(table, Math.abs(diffRows));
}
}
setValuesInTable(table, values, 1, 0);// Inserting point is the second row(index 1) and first column (index 0)
}

function setValuesInTable(table, values, startRow = 0, startCol = 0) {
for (var row = 0; row < values.length; row++) {
for (var col = 0; col < values[row].length; col++) {
if (row + startRow < table.getNumRows() && col + startCol < table.getNumColumns()) {
table.getCell(row + startRow, col + startCol).getText().setText(values[row][col]);
}
}
}
}

function addRows(table, diffRows) {
for(var i = 0; i < diffRows; i++) {
table.appendRow();
}
}

function deleteRows(table, diffRows) {
for(var i = 0; i < diffRows; i++) {
table.getRow(table.getNumRows() - 1).remove();
}
}


setValuesToSlideTable() を実行すると、Code.gs内に書いたtextの値が表に入力されます。
After executing "setValuesToSlideTable()" function, the values of the text variable written in Code.gs will be inserted into the table.


一行足りない分は addRows(table, diffRows) で追加されます。
The missing rows in the table will be added using the "addRows(table, diffRows)" function.


Reference

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

Google Slidesの表にSpreadsheetから値を入れたい - Insert Spreadsheet values into a Slides table

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

Latest post

Googleドキュメントに見出しを追加したい

今回の例では、ドキュメントの末尾に「見出しD」 を追加します。 見出しA, B, C, Dのスタイルは、見出し3 ( HEADING3 ) に設定しています。  下記Code.gsの  GOOGLE_DOCUMENT_URL を設定して  addHeadingToEnd()  を...