/** * 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('CreatedSlidewithID:'+slide.replies[0].createSlide.objectId);returnslide;}catch(e){// TODO (developer) - Handle Exceptionconsole.log('Failedwitherror%s',e.message);}}
读取页面元素对象 ID
以下示例演示了如何使用字段掩码检索特定幻灯片上每个页面元素的对象 ID。这相当于从网页读取元素对象 ID 方案示例。
/** * 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('Failedwitherror%s',e.message);}}
/** * 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':'MyAddedTextBox','insertionIndex':0}}];try{constresponse=Slides.Presentations.batchUpdate({'requests':requests},presentationId);console.log('CreatedTextboxwithID:'+response.replies[0].createShape.objectId);returnresponse;}catch(e){// TODO (developer) - Handle Exceptionconsole.log('Failedwitherror%s',e.message);}}
/** * 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('Failedwitherror%s',e.message);}}
最佳做法
批量更新
使用 Google 幻灯片高级服务时,请在数组中组合多个请求,而不是在循环中调用 batchUpdate。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2024-12-21。"],[[["The Advanced Slides service enables Apps Script to read and edit Google Slides content using the Slides API."],["This advanced service requires enabling before use and utilizes the same structure as the public Slides API."],["Provided code samples demonstrate common tasks like creating presentations and slides, manipulating elements, and formatting text."],["For optimal performance, it is recommended to batch multiple requests within a single `batchUpdate` call instead of using loops."]]],[]]