Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Google Sheets convert row in spreadsheet to Tab Delimited Text

I am trying to create a Google Sheets script that will convert a row (coming from a Spreadsheet) to tab-delimeted text.

  var sheet   = SpreadsheetApp.getActiveSpreadsheet().getSheetByName( spreadsheetname );
  var rows    = sheet.getDataRange();
  var numRows = rows.getNumRows();
  var values  = rows.getValues();

  testrow = values[5];

  Logger.log ( 'OUTPUT: testrow ');
  Logger.log ( testrow );

  var textprint = testrow.map(function (a) {return a.join('\t');}).join('\n');

  Logger.log ( 'OUTPUT: textprint ');
  Logger.log ( textprint );

Output:

Nov 3, 2022, 11:29:04 PM    Info    OUTPUT: testrow 
Nov 3, 2022, 11:29:04 PM    Info    [2113090.0, 20.0, 0.0, CHV62, X_TP_1, BTCUSD, 20482.84, Mon Oct 31 22:34:29 GMT-05:00 2022, , , , Tue Nov 01 10:19:20 GMT-05:00 2022, , 7466898.0, 1.0, ]
Nov 3, 2022, 11:29:04 PM    Error   TypeError: a.join is not a function
    at [unknown function](Code:100:54)
    at deleteRows(Code:100:27)
    at onMyEdit(Code:402:29)

What am I doing wrong?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

TIA

>Solution :

From your showing script and your log, I thought that in your script, testrow is a one-dimensional array. I thought that this might be the reason for your issue of a.join is not a function. In this case, how about the following modification?

From:

var textprint = testrow.map(function (a) {return a.join('\t');}).join('\n');

To:

var textprint = testrow.join('\t');

or

var textprint = testrow.join('\t') + "\n";

Note:

  • If you want to use the array of values, how about the following modification?

      var values = rows.getValues();
      var textprint = values.map(function (a) { return a.join('\t'); }).join('\n');
    
  • or

      var values = rows.getValues();
      var textprint = values.map(a => a.join('\t')).join('\n');
    
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading