Jeff Sawatzke
Jeff Sawatzke

Reputation: 61

Discord Bot reads Google Sheets and returns values in table. I'd like to return values ar CSV in a message without the table formatting

Hi I have a bot that I am trying to modify...

It currently displays the data in a table, but I would like it displayed as CSV in a message.

Is there a way to do this?

The data currently displays like this:

╔═════════════════════╤═════╤═══════╤═══════╗
║ Names │ Grd │ Val1 │ Val ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ a │ 400K │ 469K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ b │ 392K │ 427K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 392K │ 455K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 385K │ 394K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 366K │ 445K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 363K │ 397K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ a │ 362K │ 433K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ a │ 362K │ 400K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 358K │ 426K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 356K │ 429K ║
╟─────────────────────┼─────┼───────┼───────╢
║ tom bob jan tim tom │ U │ 354K │ 409K ║
╚═════════════════════╧═════╧═══════╧═══════╝

I would like it to display like this:

Names,Grade, Value1,Value2
tom bob jan tim tom,a,400K,469K
tom bob jan tim tom,b,392K,427K
tom bob jan tim tom,U,392K,455K
tom bob jan tim tom,U,354K,409K

Thanks in advance

The .js code is below.

const config = require('../util/globals');
const { table } = require('table');
const { GoogleSpreadsheet } = require('google-spreadsheet');

const doc = new GoogleSpreadsheet(config.SHEET_ID);
doc.useServiceAccountAuth(require('../client-secret.json'));

module.exports = {
  command: 'list',
  aliases: ['le'],
  dm: false,
  permissions: (member) => {
    return true;
  },
  async execute(bot, msg, args) {
    const abbreviations = fs
      .readFileSync('heroes.txt', 'utf-8')
      .split(/\r\n|\n/g)
      .map((row) => row.split(':')[0].toLowerCase());
    if (args.length < 4) {
      return msg.channel.send('Please at least enter 4 names.');
    }
    if (args.slice(0, 5).some((arg) => !abbreviations.includes(arg))) {
      return msg.channel.send('Unrecognised name(s). Please use all lowercase');
    }

    await doc.loadInfo();

    const sheet = doc.sheetsByTitle['Output'];
    await sheet.loadCells();

    sheet.getCellByA1('H3').value = args.slice(0, 5).join(' ');
    await sheet.saveUpdatedCells();

    const sheet2 = doc.sheetsByTitle['OutputI'];

    await sheet2.loadCells();
    const data1 = [
      [
        'Names',
        'Grd',
        'Value1',
        'value2',
      ],
      ...[...Array(15).keys()].slice(9).map((row) => {
        return [...Array(4).keys()].map(
          (column) => sheet2.getCell(row, column).value
        );
      })
    ];

    const data2 = [
      [
        'Names',
        'Grd',
        'Value1',
        'value2',
      ],
      ...[...Array(20).keys()].slice(15).map((row) => {
        return [...Array(4).keys()].map(
          (column) => sheet2.getCell(row, column).value
        );
      })
    ];

    msg.channel.send(
      '```' + table(data1).split('\n').slice(0, -2).join('\n') + '```'
    );
    msg.channel.send(
      '```' + table(data2).split('\n').slice(2).join('\n') + '```'
    );
  }
};

Upvotes: 3

Views: 687

Answers (1)

Jeff Sawatzke
Jeff Sawatzke

Reputation: 61

I figured this out. Kinda feel dumb for even asking...

msg.channel.send(
  '```' + table(data1).split('\n').slice(0, -2).join('\n') + '```'
);
msg.channel.send(
  '```' + table(data2).split('\n').slice(2).join('\n') + '```'

just had to change the above lines to:

msg.channel.send(
  (data1)
);
msg.channel.send(
  (data2)

Upvotes: 3

Related Questions