通过Google脚本更新星期几

时间:2018-12-14 16:42:40

标签: google-apps-script google-slides

我创建了以下代码来运行手动脚本,以更改Google幻灯片演示文稿中多个实例的日期。如果当天是星期一,则脚本会将其更改为星期四。如果是星期四,则其他脚本将其更改为星期一。

function futurebriefthurs() {
  var currdate = new Date();
  var daystochange = 3;
  var newbriefday = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate() + daystochange);
  var NextBrief = Utilities.formatDate(new Date(newbriefday), "GMT-7", "EEEEE");
  var pattern = "\\b\\d{1,2}/\\d{1,2}/\\d{4}\\b"; 
  var slides = SlidesApp.getActivePresentation().getSlides();
  var slidesLength = slides.length;
  for (var i = 0; i < slidesLength; i++) {  
    var shapes = slides[i].getShapes();
    var shapesLength = shapes.length;
  for (var j = 0; j < shapesLength; j++) {
     if (shapes[j].getDescription() == "$NextBrief") {
       var textRange = shapes[j].getText();
       textRange.clear();
       textRange.insertText(0, NextBrief);
      }
    }
  }
} 

function futurebriefmon() {
  var currdate = new Date();
  var daystochange = 4;
  var newbriefday = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate() + daystochange);
  var NextBrief = Utilities.formatDate(new Date(newbriefday), "GMT-7", "EEEEE");
  var pattern = "\\b\\d{1,2}/\\d{1,2}/\\d{4}\\b"; 
  var slides = SlidesApp.getActivePresentation().getSlides();
  var slidesLength = slides.length;
  for (var i = 0; i < slidesLength; i++) {  
    var shapes = slides[i].getShapes();
    var shapesLength = shapes.length;
    for (var j = 0; j < shapesLength; j++) {
      if (shapes[j].getDescription() == "$NextBrief") {
        var textRange = shapes[j].getText();
        textRange.clear();
        textRange.insertText(0, NextBrief);
      }
    }
  } 
}  

我想做的是创建一个脚本,该脚本在打开演示文稿时运行,以检查当日并自动进行相同的更改。我们在预定的日期使用此演示文稿,因此将日期更改为星期一和星期四以外的其他时间的需求降至最低。

在某个时候,我希望能够根据Google日历条目进行更改,但这是另一天的事情。 :-)

1 个答案:

答案 0 :(得分:0)

我认为类似的方法会起作用。

在安装代码时,您只需要运行createOpenTrigger()。我以为代码中的其他所有内容都在起作用。

function createOpenTrigger(){
  var triggers=ScriptApp.getProjectTriggers();
  var tA=[];
  for(var i=0;i<triggers.length;i++){tA.push(triggers[i].getHandlerFunction());}
  if(tA.indexOf('futureBrief')!=-1){
    ScriptApp.newTrigger('futureBrief').forSpreadsheet(sheet).onOpen().create();
  }
}  

function futureBrief() {
  var currdate = new Date();
  var dtcA=[0,4,0,0,3,0,0];
  var daystochange=dtcA[currdate.getDay()];
  if(daystochange>0){
    var newbriefday = new Date(currdate.getFullYear(), currdate.getMonth(), currdate.getDate() + daystochange);
    var NextBrief = Utilities.formatDate(new Date(newbriefday), "GMT-7", "EEEEE");
    var pattern = "\\b\\d{1,2}/\\d{1,2}/\\d{4}\\b"; 
    var slides = SlidesApp.getActivePresentation().getSlides();
    var slidesLength = slides.length;
    for (var i = 0; i < slidesLength; i++) {  
      var shapes = slides[i].getShapes();
      var shapesLength = shapes.length;
      for (var j = 0; j < shapesLength; j++) {
        if (shapes[j].getDescription() == "$NextBrief") {
          var textRange = shapes[j].getText();
          textRange.clear();
          textRange.insertText(0, NextBrief);
        }
      }
    }
  }
}