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

2024年5月24日金曜日

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


Google Slidesで現在選択している表に値を入れたい - Insert values into the active table
上記のリンク先のコードは、表の行数と値の数が一致していました。
In the code linked above, the number of rows in the table matched the number of values.


今回は、値の数が表と異なる場合、表の行を増やして値を入れるコードを試しました。
This time, I tried with code that dynamically adds rows to the table in Slides if the number of values doesn't match the existing table size.



今回の例では、3行3列の表を配置しておきます。
In this example, a 3x3 table will be pre-placed on the slide.

表に入れる値は、4行3列の配列を用意します。
Prepare a 4x3 array of values to insert into the table.
var values = [
['Value 1', 'Value 2', 'Value 3'],
['Value 4', 'Value 5', 'Value 6'],
['Value 7', 'Value 8', 'Value 9'],
['Value 10', 'Value 11', 'Value 12']
];



Code.gs
function insertValuesIntoSelectedTable() {
var presentation = SlidesApp.getActivePresentation();
var selection = presentation.getSelection();
var pageElement = selection.getPageElementRange().getPageElements()[0];
var table = pageElement.asTable();
var values = [
['Value 1', 'Value 2', 'Value 3'],
['Value 4', 'Value 5', 'Value 6'],
['Value 7', 'Value 8', 'Value 9'],
['Value 10', 'Value 11', 'Value 12']
];

const tableRows = table.getNumRows();

if(values.length !== tableRows) {
const diffRows = values.length - tableRows;
for(let i = 0; i < diffRows; i++) {
table.appendRow();
}
}

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


表を選択して、insertValuesIntoSelectedTable() を実行します。
Select the table and execute the insertValuesIntoSelectedTable() function.


下に一行追加されて、用意した配列の値がすべて入力されます。
A new row is added below the table, and all the values from the prepared array are inserted into it.


Reference

Class Table > appendRow()

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