0
votes

I would like to create csv file from Google spreadsheet automatically (after clicking button). But I don't want to export whole spreadsheet but just few columns. How can I do this?

eg. spreadsheets has 8 columns and I would like to export first three to csv with values like: "column1 value; column2 value; column3 value; column1 value; column2 value; ... "

Thank you

1

1 Answers

0
votes

here you go: (you need to publish that if you want it to work) it will export the selection that you are making

function onOpen() {
  var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  var entries = [{
    name : "make CSV",
    functionName : "myFunction"
  }];
  spreadsheet.addMenu("Script Center Menu", entries);
};
function myFunction() {
  var a1 = SpreadsheetApp.getActiveRange().getA1Notation();
  var url = ScriptApp.getService().getUrl()+"?ref="+a1+"&sheet="+SpreadsheetApp.getActiveSheet().getName()+"&ssid="+SpreadsheetApp.getActive().getId();
  var vals = SpreadsheetApp.getActiveRange().getValues();
  var html = "here the download Link <a href='"+url+"' >"+url+"</a>";
  //SpreadsheetApp.getActive().show(HtmlService.createHtmlOutput(html)); // ol sheet style
  SpreadsheetApp.getUi().showModalDialog(HtmlService.createHtmlOutput(html), 'download box'); // new sheet style
}


function doGet(e){
  var ref = e.parameter.ref;
  var sheet = e.parameter.sheet;
  var ssid = e.parameter.ssid;
  var out = ContentService.createTextOutput(SpreadsheetApp.openById(ssid).getSheetByName(sheet).getRange(ref).getValues().join("\n")).downloadAsFile("out.csv");
  return out;
}