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

2024年5月26日日曜日

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


以下リンク先の2と3では、それぞれ行の追加と削除を行いました。
In the linked articles below, rows were added in example 2 and deleted in example 3.
Google Slidesで現在選択している表に値を入れたい2 - Insert values into the active table2
Google Slidesで現在選択している表に値を入れたい3 - Insert values into the active table3


今回は行の追加と削除を合わせたコードを書きます。
This time, I will write code that combines both adding and removing rows.



Code.gs
function moreValuesThanRows() {
const 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'],
];
insertValuesIntoSelectedTable(values);
}

function lessValuesThanRows() {
const values = [
['Value 1', 'Value 2', 'Value 3'],
['Value 4', 'Value 5', 'Value 6']
];
insertValuesIntoSelectedTable(values);
}

function insertValuesIntoSelectedTable(values) {
const presentation = SlidesApp.getActivePresentation();
const selection = presentation.getSelection();
const pageElement = selection.getPageElementRange().getPageElements()[0];
const table = pageElement.asTable();
const tableRows = table.getNumRows();

if(values.length !== tableRows) {
const diffRows = values.length - tableRows;
if(0 < diffRows) {
addRows(table, diffRows);
} else {

deleteRows(table, Math.abs(diffRows));
}
}

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]);
}
}
}
}

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

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


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


moreValuesThanRows() を実行すると、必要な行が追加されます。
After "moreValuesThanRows()" is executed, the necessary rows will be added.

lessValuesThanRows() を実行すると、不要な行が削除されます。
After "lessValuesThanRows()" is executed, the unnecessary rows will be deleted.


Reference

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

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

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

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