英文:
Slides is not defined
问题
I can help you with the translation of the code portion:
function ReplacePgNm() {
var MainPPT = SlidesApp.openById(MainPPT_ID);
var Main = MainPPT.getSlides();
var len = Main.length;
var i = 1;
Main.forEach(function(slide) {
var pageElementId = slide.getObjectId();
console.log(pageElementId);
var resource = {
requests: [{
replaceAllText: {
pageObjectIds: [pageElementId],
replaceText: i + "/" + len,
containsText: { matchCase: true, text: "{{PgNm}}" }
}
}]
};
Slides.Presentations.batchUpdate(resource, MainPPT.getId());
i++;
})
}
Please note that the code you provided seems to contain HTML-encoded characters, so I've decoded them in the translation.
英文:
I'm doing my first steps in Google Apps script, and trying to add SlideNumbers with a code that I found.
Each slide has textbox with "{{PgNm}}" text, and I'm trying to replace this with the pattern SlideNum/NumOfSlides.
When I run it, I get "Slides is not defined" message.
Maybe someone can help me?
function ReplacePgNm() {
var MainPPT=SlidesApp.openById(MainPPT_ID);
var Main=MainPPT.getSlides();
var len = Main.length;
var i = 1;
Main.forEach(function(slide) {
var pageElementId = slide.getObjectId();
console.log(pageElementId);
var resource = {
requests: [{
replaceAllText: {
pageObjectIds: [pageElementId],
replaceText: i+"/"+len,
containsText: { matchCase: true, text: "{{PgNm}}" }
}
}]
};
Slides.Presentations.batchUpdate(resource, MainPPT.getId());
i++;
})
}
答案1
得分: 0
以下是翻译好的内容:
修改要点:
- 我认为您目前出现“Slides未定义”问题的原因是因为您从未在高级Google服务中启用Slides API。
- 而且,在您的脚本中,
Slides.Presentations.batchUpdate
在循环中使用。在这种情况下,处理成本会变得很高。
当这些要点反映在您的脚本中时,它变成如下所示。
1. 启用Slides API。
请在高级Google服务中启用Google Slides API。
2. 修改后的脚本。
function ReplacePgNm() {
var MainPPT_ID = "###"; // 请设置您的Google Slides ID。
var MainPPT = SlidesApp.openById(MainPPT_ID);
var Main = MainPPT.getSlides();
var len = Main.length;
var requests = Main.map((slide, i) => ({
replaceAllText: {
pageObjectIds: [slide.getObjectId()],
replaceText: (i + 1) + "/" + len,
containsText: { matchCase: true, text: "{{PgNm}}" }
}
}));
Slides.Presentations.batchUpdate({ requests }, MainPPT_ID);
}
或者,我认为在您的脚本中,您的目标可以仅通过Slides服务(SlidesApp)而无需使用Slides API 来实现。当这反映在示例脚本中时,它变成如下所示。在这种情况下,无需启用Slides API。
function ReplacePgNm() {
var MainPPT_ID = "###"; // 请设置您的Google Slides ID。
var MainPPT = SlidesApp.openById(MainPPT_ID);
var Main = MainPPT.getSlides();
var len = Main.length;
Main.forEach((slide, i) => slide.replaceAllText("{{PgNm}}", (i + 1) + "/" + len));
}
参考:
- Method: presentations.batchUpdate
- ReplaceAllTextRequest
- Class Slide 的 replaceAllText(findText, replaceText) 方法
英文:
Modification points:
- I think that the reason for your current issue of
Slides is not defined
is due to that you have never enabled Slides API at Advanced Google services. - And, in your script,
Slides.Presentations.batchUpdate
is used in a loop. In this case, the process cost becomes high.
When these points are reflected in your script, it becomes as follows.
1. Enable Slides API.
Please enable Google Slides API at Advanced Google services.
2. Modified script.
function ReplacePgNm() {
var MainPPT_ID = "###"; // Please set your Google Slides ID.
var MainPPT = SlidesApp.openById(MainPPT_ID);
var Main = MainPPT.getSlides();
var len = Main.length;
var requests = Main.map((slide, i) => ({
replaceAllText: {
pageObjectIds: [slide.getObjectId()],
replaceText: (i + 1) + "/" + len,
containsText: { matchCase: true, text: "{{PgNm}}" }
}
}));
Slides.Presentations.batchUpdate({ requests }, MainPPT_ID);
}
Or, I thought that in your script, your goal can be achieved by only the Slides service (SlidesApp) without using Slides API. When this is reflected in a sample script, it becomes as follows. In this case, Slides API is not required to be enabled.
function ReplacePgNm() {
var MainPPT_ID = "###"; // Please set your Google Slides ID.
var MainPPT = SlidesApp.openById(MainPPT_ID);
var Main = MainPPT.getSlides();
var len = Main.length;
Main.forEach((slide, i) => slide.replaceAllText("{{PgNm}}", (i + 1) + "/" + len));
}
References:
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论