使用Google Apps脚本访问Google Drive时出现问题

时间:2020-03-12 04:58:57

标签: google-apps-script google-drive-api

所以我用GAS为Slack编写了一个机器人,一切正常。现在,我制作了该漫游器的副本,并开始发出奇怪的错误“很抱歉,发生服务器错误。请稍候,然后重试。”当我尝试访问Google驱动器中的文件夹时。由于代码完全相同,所以我不知道问题出在哪里。

//Create Pdf
function exportCurrentSheetAsPDF_() {
  var ss = SpreadsheetApp.openById(MAIN_SPREADSHEET_ID);
  var currentSheet = ss.getSheetByName(ASAKAWA_HOLIDAY_SHEET);
  var blob = getAsBlob(ss.getUrl(), currentSheet);
  var date = Utilities.formatDate(new Date(), 'Asia/Tokyo', 'YYYYMMddhhmmss');
  var pdfName = mergePdfName("休日", date, "浅川Only", "H");

  //Export pdf to google drive 008.2 Entertainment folder
  exportPdfToDrive(blob, pdfName);
  return pdfName;
}

function exportPdfToDrive(blob, fileName) {
  blob = blob.setName(fileName);
  var folder = DriveApp.getFolderById(GOOGLE_DRIVE_FOLDER_ID);  <--- This gives error
  var pdfFile = folder.createFile(blob);
}

//Get spreadsheet as blob
function getAsBlob(url, sheet, range) {
  var exportUrl = url.replace(/\/edit.*$/, '')
      + "/export?exportFormat=pdf"
      + "&format=pdf"
      + "&size=A4"
      + "&portrait=true"
      + "&fitw=true"                  
      + "&sheetnames=false"
      + "&printtitle=false"
      + "&pagenum=false"
      + "&gridlines=false"
      + "&fzr=FALSE"      
      + "&gid=" + sheet.getSheetId();

  var response = UrlFetchApp.fetch(exportUrl, {
    headers: { 
      Authorization: 'Bearer ' +  ScriptApp.getOAuthToken(),
    },
  })

  return response.getBlob();
}

0 个答案:

没有答案