0

I would like to export the contents of the columns C & D (see red marking in the screenshot) as a text file using a button according to the scheme NYSE:GE,NYSE:BAC,... without breaks, spaces, etc.

The selection of the data should also be linked to certain conditions.

Example: Export all contents of columns C & D where the columns A, H, I and U (see blue marking in screenshot) contain something specific.

Ideally, it would be possible to set the conditions in the table using a drop-down selection, then retrieve the conditions in the code and trigger the export accordingly using a button.

Update: For my case I have changed the code from Cooper to the following. This works for me. Thanks to Cooper :-)

function cAndDtoCSV(filename) {

  var filename=filename||'Watchlist.csv';
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var rg1 =ss.getRange(""); //Get range of conditions
  var vA=rg.getValues();
  var Cd1 = rg1.getCell().getValue(); //Get value of condition No.1
  var Cd2 = rg1.getCell().getValue(); //Get value of condition No.2
  var Cd3 = rg1.getCell().getValue(); //Get value of condition No.3
  var csv="";
  for(var i=0;i<vA.length;i++) {
    if(vA[i][0]==Cd1 && vA[i][7]==Cd2 && vA[i][8]==Cd3) {  
      csv+=Utilities.formatString('%s,%s\r\n', vA[i][2],vA[i][3]);
    }
  }
  var file=DriveApp.createFile(filename,csv,MimeType.CSV);
}
Dpei
  • 53
  • 6
  • 1
    Possible duplicate of [How to export google sheet as CSV by selected columns](https://stackoverflow.com/questions/50764458/how-to-export-google-sheet-as-csv-by-selected-columns) – Sourabh Choraria Oct 02 '19 at 10:08
  • Make some attempt to write a basic code yourself and show us where you got stuck and need help – ziganotschka Oct 02 '19 at 11:03

1 Answers1

0
function cAndDtoCSV(filename) {
  var filename=filename||'myfile.csv';
  var ss=SpreadsheetApp.getActive();
  var sh=ss.getActiveSheet();
  var rg=sh.getDataRange();
  var vA=rg.getValues();
  var csv="";
  for(var i=0;i<vA.length;i++) {
    if(vA[i][0] && vA[i][7] && vA[i][8]) {
      csv+=Utilities.formatString('%s,%s\r\n', vA[i][2],vA[i][3]);
    }
  }
  var file=DriveApp.createFile(filename,csv,MimeType.CSV);
}
Cooper
  • 59,616
  • 6
  • 23
  • 54
  • Thank you for your help. I'll try to understand the code. 1.) The for loop **var i=0;i Is my understanding correct? – Dpei Oct 10 '19 at 09:35
  • vA.length is essentially the number of rows of data. vA[i][0] is the 2d array element for extracting the value in column A. Utilities.formatString() creates a string. It works a lot like C’s sprintf(). – Cooper Oct 10 '19 at 12:20
  • Thank you very much. This is exatly what I have been searching for. – Dpei Oct 11 '19 at 06:20