修改函数以下载工作表



我想修改这个函数,这样我实际上就不需要点击"立即下载"链接。我只想让脚本自动下载工作表。感谢任何帮助。谢谢。

    function copySheet() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
    var first = ss.getSheetByName("Road MT");
      ss.rename(first.getRange(2, 14).getValue());
      // this would change the name to whatever is in Row 2, col 14
     var myValue = 
    SpreadsheetApp.getActiveSpreadsheet().getActiveSheet().getRange("N2")
    .getValue();
      var copiedSpreadSheet = 
    SpreadsheetApp.getActiveSpreadsheet().copy(myValue);
      var ssID = SpreadsheetApp.getActive().getId();
      var URL = 'https://docs.google.com/spreadsheets/d/'+ssID+'/export? 
   format=xlsx';
      // Display a modal dialog box with download link.
      var htmlOutput = HtmlService
              .createHtmlOutput('<a href="'+URL+'">Download Now</a>')
              .setSandboxMode(HtmlService.SandboxMode.IFRAME)
              .setWidth(300)
              .setHeight(40);
      SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Download XLS');
    }

你可以这样做,但你需要像这样调用从doGet或doPost下载:

function doGet(fileId) {
  var URL = 'https://docs.google.com/spreadsheets/d/'+fileId+'/export?format=xlsx';  
  var fileName = 'download.xlsx';
  var blob = DriveApp.getFileById(fileId).getBlob();
  return ContentService.createTextOutput(blob).downloadAsFile(fileName);
}

最新更新