Mantieni tutto organizzato con le raccolte Salva e classifica i contenuti in base alle tue preferenze.
Il servizio Presentazioni avanzato ti consente di accedere all'API Slides utilizzando Apps Script. Questo servizio consente agli script di leggere e modificare i contenuti in Presentazioni Google.
Riferimento
Per informazioni dettagliate su questo servizio, consulta la documentazione di riferimento per l'API Slides. Come tutti i servizi avanzati in Apps Script, il servizio avanzato Presentazioni utilizza gli stessi oggetti, metodi e parametri dell'API pubblica. Per ulteriori informazioni, consulta Come vengono determinate le firme dei metodi.
Il codice di esempio riportato di seguito utilizza la versione 1 dell'API.
Crea una nuova presentazione
L'esempio seguente mostra come creare un nuovo file utilizzando il servizio avanzato di Presentazioni. È equivalente all'esempio di ricetta Crea una nuova presentazione.
/** * Create a new presentation. * @return {string} presentation Id. * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/create */functioncreatePresentation(){try{constpresentation=Slides.Presentations.create({'title':'MyNewPresentation'});console.log('Created presentation with ID: '+presentation.presentationId);returnpresentation.presentationId;}catch(e){// TODO (developer) - Handle exceptionconsole.log('Failed with error %s',e.message);}}
Crea una nuova slide
Il seguente esempio mostra come creare una nuova slide in una presentazione, in un indice specifico e con un layout predefinito. È equivalente all'esempio di ricetta Crea una nuova slide.
/** * Create a new slide. * @param {string} presentationId The presentation to add the slide to. * @return {Object} slide * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate */functioncreateSlide(presentationId){// You can specify the ID to use for the slide, as long as it's unique.constpageId=Utilities.getUuid();constrequests=[{'createSlide':{'objectId':pageId,'insertionIndex':1,'slideLayoutReference':{'predefinedLayout':'TITLE_AND_TWO_COLUMNS'}}}];try{constslide=Slides.Presentations.batchUpdate({'requests':requests},presentationId);console.log('Created Slide with ID: '+slide.replies[0].createSlide.objectId);returnslide;}catch(e){// TODO (developer) - Handle Exceptionconsole.log('Failed with error %s',e.message);}}
Leggi gli ID oggetto degli elementi di pagina
L'esempio seguente mostra come recuperare gli ID oggetto per ogni elemento della pagina di una slide specifica utilizzando una maschera di campo. È equivalente all'esempio di ricetta Leggi gli ID oggetto elemento da una pagina.
/** * Read page element IDs. * @param {string} presentationId The presentation to read from. * @param {string} pageId The page to read from. * @return {Object} response * @see https://developers.google.com/slides/api/reference/rest/v1/presentations.pages/get */functionreadPageElementIds(presentationId,pageId){// You can use a field mask to limit the data the API retrieves// in a get request, or what fields are updated in an batchUpdate.try{constresponse=Slides.Presentations.Pages.get(presentationId,pageId,{'fields':'pageElements.objectId'});console.log(response);returnresponse;}catch(e){// TODO (developer) - Handle Exceptionconsole.log('Failed with error %s',e.message);}}
Aggiungi una nuova casella di testo
L'esempio riportato di seguito mostra come aggiungere una nuova casella di testo a una slide e aggiungere testo. È equivalente all'esempio Aggiungere una casella di testo a una slide.
/** * Add a new text box with text to a page. * @param {string} presentationId The presentation ID. * @param {string} pageId The page ID. * @return {Object} response * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate */functionaddTextBox(presentationId,pageId){// You can specify the ID to use for elements you create,// as long as the ID is unique.constpageElementId=Utilities.getUuid();constrequests=[{'createShape':{'objectId':pageElementId,'shapeType':'TEXT_BOX','elementProperties':{'pageObjectId':pageId,'size':{'width':{'magnitude':150,'unit':'PT'},'height':{'magnitude':50,'unit':'PT'}},'transform':{'scaleX':1,'scaleY':1,'translateX':200,'translateY':100,'unit':'PT'}}}},{'insertText':{'objectId':pageElementId,'text':'My Added Text Box','insertionIndex':0}}];try{constresponse=Slides.Presentations.batchUpdate({'requests':requests},presentationId);console.log('Created Textbox with ID: '+response.replies[0].createShape.objectId);returnresponse;}catch(e){// TODO (developer) - Handle Exceptionconsole.log('Failed with error %s',e.message);}}
Formatta il testo di forma
Il seguente esempio mostra come formattare il testo di una forma, aggiornandone il colore, il carattere e la sottolineatura. È equivalente all'esempio di ricetta Formattare il testo in una forma o casella di testo.
/** * Format the text in a shape. * @param {string} presentationId The presentation ID. * @param {string} shapeId The shape ID. * @return {Object} replies * @see https://developers.google.com/slides/api/reference/rest/v1/presentations/batchUpdate */functionformatShapeText(presentationId,shapeId){constrequests=[{'updateTextStyle':{'objectId':shapeId,'fields':'foregroundColor,bold,italic,fontFamily,fontSize,underline','style':{'foregroundColor':{'opaqueColor':{'themeColor':'ACCENT5'}},'bold':true,'italic':true,'underline':true,'fontFamily':'Corsiva','fontSize':{'magnitude':18,'unit':'PT'}},'textRange':{'type':'ALL'}}}];try{constresponse=Slides.Presentations.batchUpdate({'requests':requests},presentationId);returnresponse.replies;}catch(e){// TODO (developer) - Handle Exceptionconsole.log('Failed with error %s',e.message);}}
Best practice
Aggiornamenti batch
Quando utilizzi il servizio avanzato Slides, combina più richieste in un array anziché chiamare batchUpdate in un ciclo.
[[["Facile da capire","easyToUnderstand","thumb-up"],["Il problema è stato risolto","solvedMyProblem","thumb-up"],["Altra","otherUp","thumb-up"]],[["Mancano le informazioni di cui ho bisogno","missingTheInformationINeed","thumb-down"],["Troppo complicato/troppi passaggi","tooComplicatedTooManySteps","thumb-down"],["Obsoleti","outOfDate","thumb-down"],["Problema di traduzione","translationIssue","thumb-down"],["Problema relativo a esempi/codice","samplesCodeIssue","thumb-down"],["Altra","otherDown","thumb-down"]],["Ultimo aggiornamento 2025-08-04 UTC."],[[["\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,["The 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\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\nThe sample code below uses [version 1](/slides/reference/rest) of the API.\n\nCreate 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```gosu\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\nCreate 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```gosu\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\nRead 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```gosu\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\nAdd 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```gosu\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\nFormat 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```gosu\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\nBatch 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 });"]]