Chrome 83 版本发布后,模态对话框中的文件下载不可用

After Chrome version 83 release, file download in modal dialog doesn't work

我使用 Google Apps 脚本来快速下载电子表格数据。

前几天突然下载功能不能用了。 我的一些可以使用该功能的同事使用 chrome 版本 81.0.4044.138(正式版) 谁不能使用该功能,chrome版本83.0.4103.61(正式版)

(右击并[另存为]还好)

我想知道怎样才能让一键下载功能重新激活

脚本如下

/**
 * 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: 
 */
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');
}

问题

无法通过模式对话框中的 link 下载文件

原因

deprecated 自 Chrome 83(因此出现问题)以来,这样做的能力。模态对话框是沙盒环境,因此您的脚本停止工作。如果你打开开发者工具(按f12),你会看到一个警告:

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.

向 link 添加 download 属性可缓解该部分,但您会遇到第二个警告:

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

做什么

截至目前,关于此事有一个open issue,并且承诺allow-downloads标志将被添加到HtmlService.XFrameOptionsMode.ALLOWALL - 你可以将选项添加到模态通过 setXFrameOptionsMode 对话并查找更新。