進階簡報服務
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
您可以使用 Advanced Slides 服務,透過 Apps Script 存取 Slides API。這項服務可讓指令碼讀取及編輯 Google 簡報中的內容。
參考資料
如要詳細瞭解這項服務,請參閱 Google Slides API 的參考說明文件。與 Apps Script 中的所有進階服務一樣,進階 Google 簡報服務使用的物件、方法和參數,都與公開 API 相同。詳情請參閱「如何判斷方法簽章」。
如要回報問題及尋求其他支援,請參閱 Google 簡報支援指南。
程式碼範例
下列程式碼範例使用 API 的第 1 版。
建立新簡報
以下範例說明如何使用 Slides 進階服務建立新簡報。這相當於「建立新簡報」食譜範例。
新建投影片
以下範例說明如何在簡報中建立新投影片,並指定索引和預先定義的版面配置。這等同於「建立新投影片」食譜範例。
讀取網頁元素物件 ID
以下範例說明如何使用欄位遮罩,擷取特定投影片上每個頁面元素的物件 ID。這相當於「從網頁讀取元素物件 ID」食譜範例。
新增文字方塊
以下範例說明如何在投影片中新增文字方塊,並在其中加入文字。這相當於「在投影片中新增文字方塊」食譜範例。
格式形狀文字
以下範例說明如何設定形狀文字的格式、更新文字顏色、字型,以及加上底線。這相當於「在圖案或文字方塊中設定文字格式」食譜範例。
最佳做法
批次更新
使用 Slides Advanced Service 時,請將多個要求合併至陣列,而非在迴圈中呼叫 batchUpdate
。
請勿在迴圈中呼叫 batchUpdate
。
var titles = ["slide 1", "slide 2"];
for (var i = 0; i < titles.length; i++) {
Slides.Presentations.batchUpdate(preso, {
requests: [{
createSlide: ...
}]
});
}
執行:使用更新陣列呼叫 batchUpdate
。
var requests = [];
var titles = ["slide 1", "slide 2"];
for (var i = 0; i < titles.length; i++) {
requests.push({ createSlide: ... });
}
Slides.Presentations.batchUpdate(preso, {
requests: requests
});
除非另有註明,否則本頁面中的內容是採用創用 CC 姓名標示 4.0 授權,程式碼範例則為阿帕契 2.0 授權。詳情請參閱《Google Developers 網站政策》。Java 是 Oracle 和/或其關聯企業的註冊商標。
上次更新時間:2025-08-31 (世界標準時間)。
[null,null,["上次更新時間:2025-08-31 (世界標準時間)。"],[[["\u003cp\u003eThe Advanced Slides service enables Apps Script to read and edit Google Slides content using the Slides API.\u003c/p\u003e\n"],["\u003cp\u003eThis advanced service requires enabling before use and utilizes the same structure as the public Slides API.\u003c/p\u003e\n"],["\u003cp\u003eProvided code samples demonstrate common tasks like creating presentations and slides, manipulating elements, and formatting text.\u003c/p\u003e\n"],["\u003cp\u003eFor optimal performance, it is recommended to batch multiple requests within a single \u003ccode\u003ebatchUpdate\u003c/code\u003e call instead of using loops.\u003c/p\u003e\n"]]],[],null,["# Advanced Slides Service\n\nThe Advanced Slides service lets you access the [Slides API](/slides) using\nApps Script. This service allows scripts to read and edit content in Google Slides.\n| This is an advanced service that must be [enabled before use](/apps-script/guides/services/advanced).\n\nReference\n---------\n\nFor detailed information on this service, see the\n[reference documentation](/slides/reference/rest) for the Slides API.\nLike all advanced services in Apps Script, the advanced Slides service uses the\nsame objects, methods, and parameters as the public API. For more information, see [How method signatures are determined](/apps-script/guides/services/advanced#how_method_signatures_are_determined).\n\nTo report issues and find other support, see the\n[Slides support guide](/slides/support).\n\nSample code\n-----------\n\nThe sample code below uses [version 1](/slides/reference/rest) of the API.\n\n### Create a new presentation\n\nThe following example demonstrates how to create a new presentation using the\nSlides advanced service. It is equivalent to the\n[Create a new presentation](/slides/samples/presentation#create_a_new_presentation)\nrecipe sample. \nadvanced/slides.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/slides.gs) \n\n```javascript\n/**\n * Create a new presentation.\n * @return {string} presentation Id.\n * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/create\n */\nfunction createPresentation() {\n try {\n const presentation =\n Slides.Presentations.create({'title': 'MyNewPresentation'});\n console.log('Created presentation with ID: ' + presentation.presentationId);\n return presentation.presentationId;\n } catch (e) {\n // TODO (developer) - Handle exception\n console.log('Failed with error %s', e.message);\n }\n}\n```\n\n### Create a new slide\n\nThe following example demonstrates how to create a new slide in a presentation,\nat a specific index and with predefined layout. It is equivalent to the\n[Create a new slide](/slides/samples/slides#create_a_new_slide)\nrecipe sample. \nadvanced/slides.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/slides.gs) \n\n```javascript\n/**\n * Create a new slide.\n * @param {string} presentationId The presentation to add the slide to.\n * @return {Object} slide\n * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate\n */\nfunction createSlide(presentationId) {\n // You can specify the ID to use for the slide, as long as it's unique.\n const pageId = Utilities.getUuid();\n\n const requests = [{\n 'createSlide': {\n 'objectId': pageId,\n 'insertionIndex': 1,\n 'slideLayoutReference': {\n 'predefinedLayout': 'TITLE_AND_TWO_COLUMNS'\n }\n }\n }];\n try {\n const slide =\n Slides.Presentations.batchUpdate({'requests': requests}, presentationId);\n console.log('Created Slide with ID: ' + slide.replies[0].createSlide.objectId);\n return slide;\n } catch (e) {\n // TODO (developer) - Handle Exception\n console.log('Failed with error %s', e.message);\n }\n}\n```\n\n### Read page element object IDs\n\nThe following example demonstrates how to retrieve the object IDs for every page\nelement on a specific slide using a field mask. It is equivalent to the\n[Read element object IDs from a page](/slides/samples/reading#read_element_object_ids_from_a_page)\nrecipe sample. \nadvanced/slides.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/slides.gs) \n\n```javascript\n/**\n * Read page element IDs.\n * @param {string} presentationId The presentation to read from.\n * @param {string} pageId The page to read from.\n * @return {Object} response\n * @see https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/get\n */\nfunction readPageElementIds(presentationId, pageId) {\n // You can use a field mask to limit the data the API retrieves\n // in a get request, or what fields are updated in an batchUpdate.\n try {\n const response = Slides.Presentations.Pages.get(\n presentationId, pageId, {'fields': 'pageElements.objectId'});\n console.log(response);\n return response;\n } catch (e) {\n // TODO (developer) - Handle Exception\n console.log('Failed with error %s', e.message);\n }\n}\n```\n\n### Add a new text box\n\nThe following example demonstrates how to add a new text box to a slide and add\ntext to it. It is equivalent to the\n[Add a text box to a slide](/slides/samples/writing#add_a_text_box_to_a_slide)\nrecipe sample. \nadvanced/slides.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/slides.gs) \n\n```javascript\n/**\n * Add a new text box with text to a page.\n * @param {string} presentationId The presentation ID.\n * @param {string} pageId The page ID.\n * @return {Object} response\n * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate\n */\nfunction addTextBox(presentationId, pageId) {\n // You can specify the ID to use for elements you create,\n // as long as the ID is unique.\n const pageElementId = Utilities.getUuid();\n\n const requests = [{\n 'createShape': {\n 'objectId': pageElementId,\n 'shapeType': 'TEXT_BOX',\n 'elementProperties': {\n 'pageObjectId': pageId,\n 'size': {\n 'width': {\n 'magnitude': 150,\n 'unit': 'PT'\n },\n 'height': {\n 'magnitude': 50,\n 'unit': 'PT'\n }\n },\n 'transform': {\n 'scaleX': 1,\n 'scaleY': 1,\n 'translateX': 200,\n 'translateY': 100,\n 'unit': 'PT'\n }\n }\n }\n }, {\n 'insertText': {\n 'objectId': pageElementId,\n 'text': 'My Added Text Box',\n 'insertionIndex': 0\n }\n }];\n try {\n const response =\n Slides.Presentations.batchUpdate({'requests': requests}, presentationId);\n console.log('Created Textbox with ID: ' +\n response.replies[0].createShape.objectId);\n return response;\n } catch (e) {\n // TODO (developer) - Handle Exception\n console.log('Failed with error %s', e.message);\n }\n}\n```\n\n### Format shape text\n\nThe following example demonstrates how to format a shape's text, updating its\ncolor, font and underlining its text. It is equivalent to the\n[Format text in a shape or textbox](/slides/samples/elements#format_text_in_a_shape_or_textbox)\nrecipe sample. \nadvanced/slides.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/advanced/slides.gs) \n\n```javascript\n/**\n * Format the text in a shape.\n * @param {string} presentationId The presentation ID.\n * @param {string} shapeId The shape ID.\n * @return {Object} replies\n * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate\n */\nfunction formatShapeText(presentationId, shapeId) {\n const requests = [{\n 'updateTextStyle': {\n 'objectId': shapeId,\n 'fields': 'foregroundColor,bold,italic,fontFamily,fontSize,underline',\n 'style': {\n 'foregroundColor': {\n 'opaqueColor': {\n 'themeColor': 'ACCENT5'\n }\n },\n 'bold': true,\n 'italic': true,\n 'underline': true,\n 'fontFamily': 'Corsiva',\n 'fontSize': {\n 'magnitude': 18,\n 'unit': 'PT'\n }\n },\n 'textRange': {\n 'type': 'ALL'\n }\n }\n }];\n try {\n const response =\n Slides.Presentations.batchUpdate({'requests': requests}, presentationId);\n return response.replies;\n } catch (e) {\n // TODO (developer) - Handle Exception\n console.log('Failed with error %s', e.message);\n }\n}\n```\n\nBest Practices\n--------------\n\n### Batch Updates\n\nWhen using the Slides Advanced Service, combine multiple requests in an array\nrather than calling `batchUpdate` in a loop.\n\nDon't --- Call `batchUpdate` in a loop. \n\n var titles = [\"slide 1\", \"slide 2\"];\n for (var i = 0; i \u003c titles.length; i++) {\n Slides.Presentations.batchUpdate(preso, {\n requests: [{\n createSlide: ...\n }]\n });\n }\n\nDo --- Call `batchUpdate` with an array of\nupdates. \n\n var requests = [];\n var titles = [\"slide 1\", \"slide 2\"];\n for (var i = 0; i \u003c titles.length; i++) {\n requests.push({ createSlide: ... });\n }\n\n Slides.Presentations.batchUpdate(preso, {\n requests: requests\n });"]]