在 Google 幻灯片演示文稿中显示进度条
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
编码水平:中级
时长:15 分钟
项目类型:编辑器插件
目标
- 了解解决方案的功能。
- 了解 Apps 脚本服务在解决方案中的作用。
- 设置脚本。
- 运行脚本。
关于此解决方案
使用此解决方案可在演示文稿的幻灯片底部添加进度条。

运作方式
该脚本会计算演示文稿中的幻灯片数量,并在每张幻灯片的底部添加一个矩形。该脚本会增加每个矩形形状的宽度,以显示幻灯片中的进度。
Apps 脚本服务
此解决方案使用以下服务:
- 幻灯片服务 - 获取演示的幻灯片,并为每张幻灯片添加一个矩形。
前提条件
如需使用此示例,您需要满足以下前提条件:
- Google 账号(Google Workspace 账号可能需要管理员批准)。
- 可访问互联网的网络浏览器。
设置脚本
- 点击以下按钮,复制进度条 Google 幻灯片演示。此解决方案的 Apps 脚本项目已附加到演示文稿。
制作副本
- 在演示中,依次点击扩展程序
>
进度条 >
显示进度条。
根据提示为脚本授权。
如果 OAuth 权限请求页面显示警告此应用未经过验证,请继续操作,依次选择高级 >
前往“{项目名称}”(不安全)。
再次点击扩展程序 >
进度条 > 显示进度条。
如需移除进度条,请依次点击扩展程序
>
进度条 > 隐藏进度条。
查看代码
如需查看此解决方案的 Apps 脚本代码,请点击下方的查看源代码:
贡献者
此示例由 Google 在 Google 开发者专家的帮助下维护。
后续步骤
如未另行说明,那么本页面中的内容已根据知识共享署名 4.0 许可获得了许可,并且代码示例已根据 Apache 2.0 许可获得了许可。有关详情,请参阅 Google 开发者网站政策。Java 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-25。
[null,null,["最后更新时间 (UTC):2025-07-25。"],[[["\u003cp\u003eThis Google Apps Script solution adds a progress bar to the bottom of Google Slides presentations to visually track progress through the slides.\u003c/p\u003e\n"],["\u003cp\u003eThe script uses the Slides service to calculate the number of slides, add a rectangle shape to each slide, and dynamically adjust the rectangle's width to represent progress.\u003c/p\u003e\n"],["\u003cp\u003eUsers can easily install the script by making a copy of the provided presentation and authorizing the script to access their Google Slides.\u003c/p\u003e\n"],["\u003cp\u003eThe progress bar can be shown or hidden using the "Progress bar" menu found under "Extensions" in Google Slides after installation.\u003c/p\u003e\n"],["\u003cp\u003eDevelopers can review and modify the source code, which is publicly available on GitHub, for customization or further development.\u003c/p\u003e\n"]]],["This solution adds a progress bar to Google Slides presentations using Apps Script. The script calculates the total slides and adds a rectangle to the bottom of each slide, increasing the rectangle's width to visually represent progress. Users copy a sample presentation, authorize the script, and then run it to create or remove the progress bars via the \"Extensions\" menu. It utilizes the Slides service to manipulate slides and shapes, adding and deleting these elements.\n"],null,["# Show progress bars in a Google Slides presentation\n\n**Coding level** : Intermediate \n\n**Duration** : 15 minutes \n\n**Project type**: Editor add-on\n\nObjectives\n----------\n\n- Understand what the solution does.\n- Understand what the Apps Script services do within the solution.\n- Set up the script.\n- Run the script.\n\nAbout this solution\n-------------------\n\nUse this solution to add a progress bar to the bottom of the slides in\nyour presentation.\n\n### How it works\n\nThe script calculates how many slides are in the presentation and adds a\nrectangle shape to the bottom of each slide. The script increases the width for\neach rectangle shape to show progress within the slides.\n\n### Apps Script services\n\nThis solution uses the following service:\n\n- [Slides service](https://developers.google.com/apps-script/reference/slides)--Gets a presentation's slides and adds a rectangle shape to each one.\n\nPrerequisites\n-------------\n\nTo use this sample, you need the following prerequisites:\n\n- A Google Account (Google Workspace accounts might require administrator approval).\n- A web browser with access to the internet.\n\nSet up the script\n-----------------\n\n1. Click the following button to make a copy of the **Progress bar** Slides presentation. The Apps Script project for this solution is attached to the presentation. \n [Make a copy](https://docs.google.com/presentation/d/1wMwCeISpBgSal0K1K8qXPdr-MYZxkU_A-RobUIYZ8WU/copy)\n2. In the presentation, click **Extensions** \\\u003e **Progress bar** \\\u003e **Show progress bar**.\n3. When prompted, authorize the script.\n If the OAuth consent screen displays the warning, **This app isn't verified** ,\n continue by selecting **Advanced** \\\u003e\n **Go to {Project Name} (unsafe)**.\n\n4. Again, click **Extensions** \\\u003e\n **Progress bar** \\\u003e **Show progress bar**.\n\n5. To remove the progress bar, click **Extensions**\n \\\u003e\n **Progress bar** \\\u003e **Hide progress bar**.\n\nReview the code\n---------------\n\nTo review the Apps Script code for this solution, click\n**View source code** below: \n\n#### View source code\n\n\u003cbr /\u003e\n\nslides/progress/progress.gs \n[View on GitHub](https://github.com/googleworkspace/apps-script-samples/blob/main/slides/progress/progress.gs) \n\n```javascript\n/**\n * @OnlyCurrentDoc Adds progress bars to a presentation.\n */\nconst BAR_ID = 'PROGRESS_BAR_ID';\nconst BAR_HEIGHT = 10; // px\n\n/**\n * Runs when the add-on is installed.\n * @param {object} e The event parameter for a simple onInstall trigger. To\n * determine which authorization mode (ScriptApp.AuthMode) the trigger is\n * running in, inspect e.authMode. (In practice, onInstall triggers always\n * run in AuthMode.FULL, but onOpen triggers may be AuthMode.LIMITED or\n * AuthMode.NONE.)\n */\nfunction onInstall(e) {\n onOpen();\n}\n\n/**\n * Trigger for opening a presentation.\n * @param {object} e The onOpen event.\n */\nfunction onOpen(e) {\n SlidesApp.getUi().createAddonMenu()\n .addItem('Show progress bar', 'createBars')\n .addItem('Hide progress bar', 'deleteBars')\n .addToUi();\n}\n\n/**\n * Create a rectangle on every slide with different bar widths.\n */\nfunction createBars() {\n deleteBars(); // Delete any existing progress bars\n const presentation = SlidesApp.getActivePresentation();\n const slides = presentation.getSlides();\n for (let i = 0; i \u003c slides.length; ++i) {\n const ratioComplete = (i / (slides.length - 1));\n const x = 0;\n const y = presentation.getPageHeight() - BAR_HEIGHT;\n const barWidth = presentation.getPageWidth() * ratioComplete;\n if (barWidth \u003e 0) {\n const bar = slides[i].insertShape(SlidesApp.ShapeType.RECTANGLE, x, y,\n barWidth, BAR_HEIGHT);\n bar.getBorder().setTransparent();\n bar.setLinkUrl(BAR_ID);\n }\n }\n}\n\n/**\n * Deletes all progress bar rectangles.\n */\nfunction deleteBars() {\n const presentation = SlidesApp.getActivePresentation();\n const slides = presentation.getSlides();\n for (let i = 0; i \u003c slides.length; ++i) {\n const elements = slides[i].getPageElements();\n for (const el of elements) {\n if (el.getPageElementType() === SlidesApp.PageElementType.SHAPE &&\n el.asShape().getLink() &&\n el.asShape().getLink().getUrl() === BAR_ID) {\n el.remove();\n }\n }\n }\n}\n```\n\n\n\u003cbr /\u003e\n\nContributors\n------------\n\nThis sample is maintained by Google with the help of Google Developer Experts.\n\nNext steps\n----------\n\n- [Extending Google Slides with Apps Script](/apps-script/guides/slides)\n- [Slides service reference](/apps-script/reference/slides)"]]