Tôi có ứng dụng web được phát triển bằng Google App Script HtmlService
và từ biểu mẫu html, điền bảng excel vào ổ Google bằng cách sử dụng SpreadsheetApp
. Và một phần khác đang gọi số ContentService
để tải xuống dữ liệu dưới dạng tệp excel.Tải xuống Google App Script ContentServiceAsFile không hoạt động
function doGet(e) {
// Read excel sheet
//getAppFile();
// Render the application from HTML template
return HtmlService.createTemplateFromFile('index').evaluate()
.setTitle('Go Smart')
.setSandboxMode(HtmlService.SandboxMode.IFRAME);
}
function downloadDoubleQuateCsvFile() {
var sheetId = PropertiesService.getScriptProperties().getProperty('sheetId');
var ss = SpreadsheetApp.openById(sheetId).getActiveSheet();
//var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var maxColumn = ss.getLastColumn();
var maxRow = ss.getLastRow();
var data = ss.getRange(1, 1, maxRow, maxColumn).getValues();
if (data.length > 1) {
var csv = "";
for (var row = 0; row < data.length; row++) {
for (var col = 0; col < data[row].length; col++) {
if (data[row][col].toString().indexOf(",") != - 1) {
data[row][col] = "\"" + data[row][col] + "\"";
}
}
if (row < data.length - 1) {
csv += data[row].join(",") + "\r\n";
} else {
csv += data[row];
}
}
csvFile = csv;
}
return makeCSV(csvFile);
}
function makeCSV(csvString) {
var csvFileName = 'test.csv';
var output = ContentService.createTextOutput();
output.setMimeType(ContentService.MimeType.CSV);
output.setContent(csvString);
output.downloadAsFile(csvFileName);
return output;
}
Tập lệnh này chỉ cung cấp đối tượng chi tiết tiêu đề trang tính trong bảng điều khiển và không tải xuống bất kỳ tệp nào.
<button class="btn btn-success btn-sm" onclick="google.script.run.downloadDoubleQuateCsvFile()">Export</button>
Sau khi thêm trở lại hàm thứ hai, tôi nhận được lỗi như thế này.
Error: The script completed but the returned value is not a supported return type.
Lưu ý: Tôi có file excel trong ổ đĩa với dữ liệu
Điều này sẽ chỉ hoạt động trở lại từ doGet() hoặc doPost() được yêu cầu từ url được xuất bản. –
@SpencerEaston Từ doGet() Tôi đã tạo và trả lại HtmlService. Có cách nào khác để đạt được nó? – devo
Tôi không thấy doGet() trong ví dụ của bạn. Và btw trong ví dụ của bạn, bạn không 'return' makeCSV (csvFile). –