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

我怕爱的太早我们不能终老 提交于 2019-12-11 10:24:51

问题


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());

}

回答1:


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.




回答2:


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());

}


来源:https://stackoverflow.com/questions/55603383/im-trying-to-send-data-from-a-cell-range-to-a-discord-chat-channel

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!