Jac*_*b J 3 google-apps-script google-slides
当 Google 幻灯片中的某些对象隐藏在另一个对象后面时,以后可能很难在幻灯片上找到它们。例如,是否可以看到一个面板,其中包含给定幻灯片上存在的所有对象的列表?即使它们位于底层(完全隐藏在另一个对象后面),也可能编辑它们吗?当稍后显示对象并完全覆盖先前显示的对象时,这对于动画可能很有用。
我相信你的目标如下。
在这种情况下,我认为当使用Google Apps脚本创建的侧边栏来更改文本时,您的目标也许可以轻松实现。
示例脚本如下。
请将以下脚本复制并粘贴到 Google 幻灯片的脚本编辑器中并保存脚本。然后,请重新打开 Google 幻灯片。这样,就为 Google 幻灯片创建了自定义菜单“示例”。当自定义菜单“sample”中的“RUN”被打开时,脚本就会运行。
请将此脚本复制并粘贴为Code.gs.
function onOpen() {
SlidesApp.getUi().createMenu("sample").addItem("RUN", "openSidebar").addToUi();
}
function openSidebar() {
const html = HtmlService.createHtmlOutputFromFile("index").setTitle("sample");
SlidesApp.getUi().showSidebar(html);
}
function getSelectedShapes() {
const select = SlidesApp.getActivePresentation().getSelection();
const pageElementRange = select.getPageElementRange();
if (pageElementRange) {
const obj = pageElementRange.getPageElements().reduce((ar, e) => {
if (e.getPageElementType() == SlidesApp.PageElementType.SHAPE) {
const shape = e.asShape();
ar.push({objectId: shape.getObjectId(), text: shape.getText().asString().trim()});
}
return ar;
}, []).reverse();
return obj;
}
return [];
}
function updatedTexts(o) {
const select = SlidesApp.getActivePresentation().getSelection();
const slide = select.getCurrentPage();
const obj = slide.getShapes().reduce((o, e) => Object.assign(o, {[e.getObjectId()]: {shape: e, text: e.getText().asString().trim()}}), {});
o.forEach(({objectId, text}) => {
if (obj[objectId] && obj[objectId].text != text) {
obj[objectId].shape.getText().setText(text);
}
});
return "Done";
}
Run Code Online (Sandbox Code Playgroud)
请将此脚本复制并粘贴为index.html.
<input type="button" id="main" value="Get selected shapes" onClick="main()">
<div id="shapes"></div>
<input type="button" id="update" value="Updated texts" onClick="updatedTexts()" style="display:none">
<script>
function main() {
document.getElementById("main").disabled = true;
document.getElementById("shapes").innerHTML = "";
google.script.run.withSuccessHandler(o => {
if (o.length == 0) {
document.getElementById("update").style.display = "none";
return;
}
const div = document.getElementById("shapes");
o.forEach(({objectId, text}) => {
const input = document.createElement("input");
input.setAttribute("type", "text");
input.setAttribute("id", objectId);
input.setAttribute("value", text);
div.appendChild(input);
});
document.getElementById("update").style.display = "";
document.getElementById("main").disabled = false;
}).getSelectedShapes();
}
function updatedTexts() {
const inputs = document.getElementById("shapes").getElementsByTagName('input');
const obj = [...inputs].map(e => ({objectId: e.id, text: e.value}));
console.log(obj)
google.script.run.withSuccessHandler(e => console.log(e)).updatedTexts(obj);
}
</script>
Run Code Online (Sandbox Code Playgroud)
请重新打开 Google 幻灯片。至此,自定义菜单就创建完成了。请打开“示例”->“运行”。这样,侧边栏就打开了。
另外,您可以通过以下演示影片来了解它。