Google表格脚本:覆盖导出的PDF,而不是新建

时间:2019-03-07 15:12:22

标签: google-sheets-api

我试图覆盖或创建新的驱动器文件版本,而不是保存另一个具有重复名称的文件。该脚本可以正确导出,但是会创建无数相同名称的文件。

该如何解决?

 function exportSpreadsheet() {

  var ssID = "<SHEET ID>";
  var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export"+
                                                        "?format=pdf&"+
                                                        "size=0&"+
                                                        "fzr=true&"+
                                                        "portrait=true&"+
                                                        "fitw=true&"+
                                                        "gridlines=false&"+
                                                        "printtitle=false&"+
                                                        "sheetnames=false&"+
                                                        "pagenum=UNDEFINED&"+
                                                        "gid=<SHEET IT>"+
                                                        "attachment=true"+
                                                        "&top_margin=0.5&left_margin=0.5&right_margin=0.5&bottom_margin=0.5";


  var params = {method:"GET",headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}};

  var response = UrlFetchApp.fetch(url, params).getBlob().setName("Current_Cargo_Schedule");

  // save to drive
  DriveApp.createFile(response);

  // save to drive
  var dir = DriveApp.getFolderById("<FOLDER ID>");
  var file = dir.createFile(response);

  //or send as email
  /*
  MailApp.sendEmail(email, subject, body, {
        attachments: [{
            fileName: "TPS REPORT" + ".pdf",
            content: response.getBytes(),
            mimeType: "application/pdf"
        }]
    });};
  */

}

0 个答案:

没有答案