Ted Jones
Ted Jones

Reputation: 73

I'm trying to send data from a cell range to a discord chat channel

I'm trying to send data from a range of cells to a discord text channel. I have the webhook working. However, I can't seem to convert the cell data in the "message = messager" part of the code.

function postMessageToDiscord(message) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Loot");
  var range = sheet.getRange("A12:N12");



  message = message || "range" ;

  var discordUrl = 'https://discordapp.com/api/webhooks/565294544682221617/l_mER5nwxITlaW-9g0qXZxZ0CDSWqLrHYXDcvcdyubC9VElWmbfbRTdwbQhVFdyVYxFq';
  var payload = JSON.stringify({content: message});

  var params = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    payload: payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(discordUrl, params);

  Logger.log(response.getContentText());

}

Upvotes: 1

Views: 775

Answers (2)

Ted Jones
Ted Jones

Reputation: 73

This is the final result.

function postMessageToDiscord(message) {

  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var sheet = ss.getSheetByName("Loot");
  var range = sheet.getRange("A12:N12");

  var data = range.getValues();
  var result = '';

  for (var i = 0; i < data.length; i++) {
    var d = data[i];
    for (var j = 0; j < d.length; j++) {
      result = result.concat(d[j]);
   }
}


  message = message || result ;

  var discordUrl = 'https://discordapp.com/api/webhooks/565294544682221617/l_mER5nwxITlaW-9g0qXZxZ0CDSWqLrHYXDcvcdyubC9VElWmbfbRTdwbQhVFdyVYxFq';
  var payload = JSON.stringify({content: message});

  var params = {
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded'
    },
    method: "POST",
    payload: payload,
    muteHttpExceptions: true
  };

  var response = UrlFetchApp.fetch(discordUrl, params);

  Logger.log(response.getContentText());

}

Upvotes: 2

Matt
Matt

Reputation: 2213

getRange returns a Range object.

From the docs we can see there's a getValues() method which returns a two dimensional array containing the data in each cell.

So we can write a simple for loop like so:

var data = range.getValues();
var result = '';

for (var i = 0; i < data.length; i++) {
   var d = data[i];
   for (var j = 0; j < d.length; j++) {
       result = result.concat(d[j]);
   }
}

then send the result var.

Upvotes: 0

Related Questions