获取最新副本并将其通过电子邮件发送给自己

时间:2019-01-21 00:15:59

标签: javascript email google-apps-script google-sheets google-drive-api

我刚开始没有经验的学习Google脚本。我在获取原始文档的新创建副本并将其发送给自己时遇到了麻烦。由于某种原因,它会继续发送原始邮件。

function Every Week() {  


ScriptApp.newTrigger("Weekly")  
.timeBased()  
.everyDays(7)  
.create();  
var sheet = SpreadsheetApp.getActive();  
var destFolder = DriveApp.getFolderById("xxxxxxx");   
DriveApp.getFileById(sheet.getId()).makeCopy("Loads and 
date"+sheet.getName(), destFolder);     

var files = DriveApp.getFiles();  
while (files.hasNext())   
var file = files.next();  
var id = file.getId();  
var lastUpdated = DriveApp.getFileById(id).getLastUpdated();  

var MyLink = sheet.getUrl()   
var MyUser = Session.getActiveUser().getEmail();  
var files = DriveApp.g  
MailApp.sendEmail({ to: MyUser, subject: 'Weekly title', body: MyLink, }); 
} ;

我一直在获取原始模板,而不是新副本。

1 个答案:

答案 0 :(得分:0)

尝试一下:

我认为这就是您要完成的工作。

function Weekly() {
  var Found=false;
  var trgs=ScriptApp.getProjectTriggers();
  for(var i=0;i<trgs.length;i++){
    if(trgs[i].getHandlerFunction()=="Weekly"){
      Found=true;
    }
  }
  if(!Found){
    ScriptApp.newTrigger("Weekly").timeBased().everyDays(7).create();  
  }
  var spreadsheet=SpreadsheetApp.getActive();  
  var destFolder = DriveApp.getFolderById("id");   
  var newSpreadsheet=DriveApp.getFileById(spreadsheet.getId()).makeCopy("New Title", destFolder);     
  var MyLink = spreadsheet.getUrl()   
  var MyUser = Session.getActiveUser().getEmail();    
  MailApp.sendEmail(MyUser,'Weekly title',MyLink); 
}