how to combine all data in 1 column

37 views
Skip to first unread message

nagastar

unread,
Aug 2, 2023, 1:18:05 AM8/2/23
to Google Apps Script Community
I want to combine the data in column B in google sheets. after that, all the data will be sent to the chatwork chat application with one chat notification only.
this is my code:
 function sendAllRowsToChatworkExceptLastRow() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName('GAS');

  const lastRow = sheet.getLastRow();
  const lastColumn = sheet.getLastColumn();

  for (let i = lastRow; i > 1; i--) {

    const rowValues = sheet.getRange(i, 1, 1, lastColumn).getValues()[0];


    // Create a message to send to ChatWork.
    const message = `[info][title]this is task today; [/title]
      ${rowValues.join(", ")}

      [/info]`;

    // Send the message to ChatWork.
    const client = ChatWorkClient.factory({token: "0c0219265533e71d160bda27ec3f"});

    client.sendMessage({room_id: "room_Id", body: message});
  }
}

Currently, I have created the code but the problem now is that if there are 3 rows of data in column B, it will send a message 3 times to chatwork consecutively.
like this picture from the chatwork chat application:
caccc.PNG


 i want if there is data in column B for 3 rows then it will combine the data in the 3 rows of column B and send it to the chatwork application in just one message.
This is roughly the expected result:

this is task today :
cleaning room,
cleaning toilets,
cleaning garden

Jiří Janda

unread,
Aug 2, 2023, 6:39:19 AM8/2/23
to Google Apps Script Community
Try this:

function sendAllRowsToChatworkExceptLastRow() {
  const spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
  const sheet = spreadsheet.getSheetByName('GAS');

  const lastRow = sheet.getLastRow();
  const lastColumn = sheet.getLastColumn();

  let combinedMessage = ''; // Initialize an empty string to store the combined message.


  for (let i = lastRow; i > 1; i--) {
    const rowValues = sheet.getRange(i, 2).getValue(); // Get the value from column B (index 2).

    // Append the current row value to the combined message.
    combinedMessage += rowValues + "\n";
  }

  // Create a message to send to ChatWork with the combined data from column B.

  const message = `[info][title]this is task today; [/title]
      ${combinedMessage}

      [/info]`;

  // Send the message to ChatWork.
  const client = ChatWorkClient.factory({token: "0c0219265533e71d160bda27ec3f"});
  client.sendMessage({room_id: "room_Id", body: message});
}

Reply all
Reply to author
Forward
0 new messages