Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

google apps script - After Chrome version 83 release, file download in modal dialog doesn't work

I use Google Apps Script to make download Spreadsheet data quickly.

Since several days before, download function doesn't work suddenly. Some of my coworkers who can use the function use chrome version 81.0.4044.138(Official Build) and who cannot use the function , chrome version 83.0.4103.61(Official Build)

(right-click and [save as] works fortunately)

I want to know what should I do to make one-click download function active again.

Script is as follows.

/**
 * Adds a custom menu
 *
 * @param {Object} e The event parameter for a simple onOpen trigger.
 */
function onOpen(e) {
  SpreadsheetApp.getUi()
      .createMenu('Custom')
      .addItem('Download as XLSX', 'downloadXLS_GUI')
      .addToUi();
}


/**
 * Display a modal dialog with a single download link.
 *
 * From: http://stackoverflow.com/a/37336778/1677912
 */
function downloadXLS_GUI() {
  // Get current spreadsheet's ID, place in download URL
  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+'">Click to download</a>')
                  .setSandboxMode(HtmlService.SandboxMode.IFRAME)
                  .setWidth(800)
                  .setHeight(600);
  SpreadsheetApp.getUi().showModalDialog(htmlOutput, 'Download XLS');
}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Problem

Cannot download a file via link in modal dialog

Reason

The ability to do so is deprecated since Chrome 83 (hence the issue). Modal dialogs are sandbox environments, therefore your script stopped working. If you open the developer tools (press f12), you will see a warning:

Download is disallowed. The frame initiating or instantiating the download is sandboxed, but the flag ‘allow-downloads’ is not set. See https://www.chromestatus.com/feature/5706745674465280 for more details.

Adding a download attribute to the link mitigates that part, but you will encounter the second warning:

Resource interpreted as Document but transferred with MIME type application/vnd.openxmlformats-officedocument.spreadsheetml.sheet

What to do

As of now, there is an open issue regarding the matter, and it is promised that the allow-downloads flag will be added to HtmlService.XFrameOptionsMode.ALLOWALL - you can add the option to modal dialog via setXFrameOptionsMode and look for the updates.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...