0

I have an application made on google than handdle different sheets on a google workbook. I need to add a button to allow users download some of these sheets but in an excel format if is possible. I took one code from StackOv and try to modify it, as i don´t want it name the file and save it in drive, i only need it download as same that when from excel it is download in Book1 ("libro1") without saving anywhere. THK!

var ss = SpreadsheetApp.openById("fffffffffffffffffffffffffffff...myID");
var sheetId = ss.ss.getSheetByName('nameSheetNeedToDownload');
var url = "https://docs.google.com/spreadsheets/d/" + sheetId + "/export?format=xlsx&access_token=" + ScriptApp.getOAuthToken();
var blob = UrlFetchApp.fetch(url).getBlob().setName(name + ".xlsx");
createFile(blob); // here need to create but without saving
Tanaike
  • 181,128
  • 11
  • 97
  • 165
  • About downloading a file by running script, this thread might be useful. https://stackoverflow.com/questions/37212135/download-automatically-spreadsheet-as-xlsx-to-local-machine-on-event – Tanaike Mar 25 '19 at 23:11
  • 1
    Possible duplicate of [Download "automatically" spreadsheet as .xlsx to local machine on event](https://stackoverflow.com/questions/37212135/download-automatically-spreadsheet-as-xlsx-to-local-machine-on-event) – tehhowch Mar 26 '19 at 01:10
  • Hi Tanaike, THK you again!...i have tried with this script you showed me but the following error pop appear "Request failed for https://docs.google.com/spreadsheets/d/Sheet/export?format=xlsx returned code 404. Truncated server " ...any idea? Marina – MarinaMontero Mar 27 '19 at 13:42
  • @MarinaMontero Thank you for replying. Can you show us your current script which has the error? If you can do, please add it to your question. I would like to confirm it. – Tanaike Mar 27 '19 at 22:45
  • THK you Tanaike! yes, i copy the code i'm triying – MarinaMontero Mar 29 '19 at 18:44
  • function downloadAsXlsx() { var bogus = DriveApp.getRootFolder(); var spreadSheet = SpreadsheetApp.openById('WorkbookID'); var ssID = spreadSheet.getSheetByName('Respuestas de formulario 1'); Logger.log(ssID); var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?format=xlsx"; var params = {method:"GET", headers:{"authorization":"Bearer "+ ScriptApp.getOAuthToken()}}; var response = UrlFetchApp.fetch(url, params); // save to drive DriveApp.createFile(response); } – MarinaMontero Mar 29 '19 at 18:47
  • @MarinaMontero Thank you for replying. If you can do, please add the script to your question by editing button. By this, other users can see your current script in the question. I have one question. You want to download the XLSX file when you run the script. If my understanding is correct, the script is required to be the container-bound script of Google Docs (For example, it's Spreadsheet). How about this? And when you reply to my comment, please use ``@Tanaike`` in the reply. By this, your reply is notified to me. – Tanaike Mar 29 '19 at 23:58

1 Answers1

0

THK you Tanaike!...Yes here i copy the code i have

function downloadAsXlsx() {
var bogus = DriveApp.getRootFolder();
var spreadSheet = SpreadsheetApp.openById('WorkbookID'); 
var ssID = spreadSheet.getSheetByName('SheetName');

Logger.log(ssID);

var url = "https://docs.google.com/spreadsheets/d/"+ssID+"/export?format=xlsx";   
var params = {method:"GET", headers:{"authorization":"Bearer "+ 
ScriptApp.getOAuthToken()}};
var response = UrlFetchApp.fetch(url, params);

 // save to drive
DriveApp.createFile(response);

}