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

2024年5月13日月曜日

Google Slidesの表をApps Scriptで取得したい - Get tables in Slides


Googleスライドに配置した表と、表に含まれる値を取得したくて試したコードです。
I tried to get the tables and their values in a Google Slide via Apps Script.



今回使うスライドの例
The slide for this example.


スライドに3行3列の表を配置して値を入れておきます。
Place a 3x3 table on a slide and enter some values.


例では1〜9までの数字を順番に振っています。
In this example, I filled the table with numbers 1 to 9.



Code.gs
function getTableData() {
var presentation = SlidesApp.getActivePresentation();
var slide = presentation.getSelection().getCurrentPage();
var tables = slide.getTables();
var table = tables[0]; // 最初の表を取得

var numRows = table.getNumRows();
var numCols = table.getNumColumns();

for (var i = 0; i < numRows; i++) {
for (var j = 0; j < numCols; j++) {
var cell = table.getCell(i, j);
var text = cell.getText().asString();
Logger.log(text); // 取得したセルのテキストをログに出力
}
}
}


getTableData() を実行すると、以下のようなログが出力されます。
Running getTableData() outputs the following log.



Tips

Geminiに質問して得た回答をコンパクトにしたのが、上記の Code.gs です。
The Code.gs above is a compact version of the answer I got from Gemini.




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