Home > Back-end >  Google Sheets convert row in spreadsheet to Tab Delimited Text
Google Sheets convert row in spreadsheet to Tab Delimited Text

Time:11-04

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?

TIA

CodePudding user response:

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');
    
  • Related