在Google幻灯片中,我可以使用此脚本将今天的日期添加到我的第一张幻灯片中,但是下次运行时,它会添加另一个文本框,该日期的日期位于另一个日期的上方,每个日期都位于两个日期的上方其他,一团糟。我找不到在写入新日期之前将其删除或覆盖的方法。
写日期的代码:
function myFunction() {
var date = new Date();
var months = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"]
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 300, 200, 300, 60);
var textRange = shape.getText();
textRange.setText(months[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear());
谢谢
答案 0 :(得分:1)
如果我的理解正确,那么使用文本框的“标题”怎么办?请认为这只是几个答案之一。修改后的示例脚本如下。在此修改中,文本框的标识名称由文本框的标题使用。该脚本的流程如下。
function myFunction() {
var title = "sampleTitle"; // Please set this.
var date = new Date();
var months = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
var slide = SlidesApp.getActivePresentation().getSlides()[0];
var date = months[date.getMonth()]+' '+date.getDate()+', '+date.getFullYear();
var shapes = slide.getShapes();
var s = shapes.filter(function(e) {return e.getTitle() == title});
if (s.length > 0) {
s[0].getText().setText(date);
} else {
var shape = slide.insertShape(SlidesApp.ShapeType.TEXT_BOX, 300, 200, 300, 60).setTitle(title);
var textRange = shape.getText();
textRange.setText(date);
}
}