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

2024年5月16日木曜日

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


以前の記事では、スライド上にダイアログを作って表に値を入力するというのをやりました。
In a previous article, I created a dialog on a slide and used it to input values into a table.


今回は、スプレッドシートから値を取得して、それをスライドの表の中へ入力するというのを試しました。
This time, I tried retrieving values from a spreadsheet and inserting them into a table in a slide.



Spreadsheet
Slideに渡すのはシートのB2:E5に入力されている値
Insert the values from range B2:E5 in the Sheet into the Slides table.



Slide
スライドには、5列5行の表を配置しておく
Place a 5x5 table on the slide.

2列目2行目以降に上記シートの値を入力する←Code.gsはこれをやる
Insert the values starting at the second row and second column of the table.←Code.gs



Code.gs
function setValuesToSlideFromSheet() {
const sheetUrl = "https://docs.google.com/spreadsheets/d/SPREADSHEET_ID/edit#gid=SHEET_ID";
const slideUrl = "https://docs.google.com/presentation/d/SLIDE_ID/edit#slide=id.PAGE_ID";

const sheet = returnSheet(sheetUrl);
const startRow = 2;
const startCol = 2;
const numRows = 4;
const numCols = 4;
const range = sheet.getRange(startRow, startCol, numRows, numCols);
const values = range.getValues();

const page = returnPage(slideUrl);
const table = page.getTables()[0];
setValuesInTable(table, values, 1, 1);
}

function returnSheet(sheetUrl) {
const spreadsheet = SpreadsheetApp.openByUrl(sheetUrl);
const sheetId = sheetUrl.split("/edit#gid=")[1];
const sheets = spreadsheet.getSheets();
for (let i = 0; i < sheets.length; i++) {
if (sheets[i].getSheetId() == sheetId) {
return spreadsheet.getSheets()[i];
}
}
}

function returnPage(slideUrl) {
const slide = SlidesApp.openByUrl(slideUrl);
const slideId = slideUrl.split("/edit#slide=id.")[1];
const pages = slide.getSlides();
for(let i = 0; i < pages.length; i++) {
if(pages[i].getObjectId() == slideId) {
return slide.getSlides()[i];
}
}
}

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

sheetUrlとslideUrlを書き換えて、setValuesToSlideFromSheet()を実行すると、startRow, startCol, numRows, numCols で設定した範囲内にあるシートの値がSlideの表に入力されます。
After changing sheetUrl and slideUrl in Code.gs and executing setValuesToSlideFromSheet(), the values within the sheet range specified by startRow, startCol, numRows, and numCols will be inserted into the table on the Slide.


Tips

今回使うデータの作成にはGeminiを利用しました。
The data in this example was generated by Gemini.

INPUT
get
take
make
give
上記の英語について、一行ずつ以下を行ってください。
1. 日本語訳
2. Part of Speech: v. adv. adj. など品詞の短縮形
3. short description: 短い一文の英文で説明してください。
OUTPUT例 それぞれ以下の形で一行で書いてください。
get: 得る: Part of Speech: Short description

OUTPUT
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.



A列にOUTPUTを貼り付けて、B列に=split(A2,": ",false)のような関数を入れるとB,C,D,E列に値が入ります。
By pasting the OUTPUT into column A and entering a function like =split(A2, ": ", false) in column B, the values will be split and placed into columns B, C, D, and E.


Reference

Google Apps Scriptでスライド内のページIDを取得したい(.getObjectId)

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

Google Slidesの表に値を入れたい Insert values into the specific table

シートIDを渡してシートを取得する

Google Slidesのダイアログから表に値を入力したい - Input values from dialog to table

Google Slidesのダイアログから表に値を入力したい2 - Input values from dialog to table 2

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