Disclaimer I am a newbie at programing and also not really good at english, so please pardon my bad explanation.
My bot had to send a message containing data retrieved from a spreadsheet, but found that the data was so many that it exceeded the character limit of the telegram message (4096 characters).
So I assume that I have to split the message, then send them in sequence. Please help...
here's my code :
function SearchbyProjectName(Prjct){
var projectNON = GetAllProjectNON();
var project = GetAllProject();
var num = 1;
const allLocation = [];
var header =
'xxxheaderxxx';
var footer =
'xxxfooterxxx'
for (var row=0; row<projectNON.length; row++) {
if(projectNON[row][3]==Prjct){
var resiNON = num++ + '. ) ' + '/d_' + projectNON[row][6] + '\n\n';
allLocation.push(resiNON);
}
}
for (var row=0; row<project.length; row++) {
if(project[row][3]==Prjct){
var resi = num++ + '. ) ' + '/d_' + project[row][6] + '\n\n';
allLocation.push(resi);
}
}
var stringg = allLocation.toString();
var clearComma = stringg.replaceAll(",", "")
var newData = header + clearComma + footer;
return newData ;
}
here's the message :
ALL LOCATION
Project: PTT2
🗓 : Tuesday, 24 Jan 2023 15:10:34
) /d_XXXXXXXXXXX
) /d_XXXXXXXXXXXXX
) /d_XXXX
) /d_DDDDDD
) /d_JJJJJJJJJJJJJ
) /d_XXXXXXX
...
) /d_BBBBBBBBBBBBB
I wish I could split the message at number 100.)
I'm not able to test this but I think it might solve your problem. For every 100 projects store the string in the array newData. Then in the receiver of newData loop throught the array of strings and send each individually.
Notice I don't push strings to the array allLocation but simply concatinate strings. Once I've reach a multiple of 100 push that concatinated string to the array newData.
function SearchbyProjectName(Prjct){
var projectNON = GetAllProjectNON();
var project = GetAllProject();
var num = 1;
var newData = [];
var allLocation = "xxxheaderxxx";
var footer = "xxxfooterxxx";
var limit = 100;
for (var row=0; row<projectNON.length; row++) {
if(projectNON[row][3]==Prjct){
var resiNON = num++ + '. ) ' + '/d_' + projectNON[row][6] + '\n\n';
allLocation.concat(resiNON);
if( num === limit ) {
newData.push(allLocation);
limit = limit+100;
allLocation = "";
}
}
}
for (var row=0; row<project.length; row++) {
if(project[row][3]==Prjct){
var resi = num++ + '. ) ' + '/d_' + project[row][6] + '\n\n';
allLocation.concat(resi);
if( num === limit ) {
newData.push(allLocation);
limit = limit+100;
allLocation = "";
}
}
}
allLocation = allLocation.concat(footer);
newData.push(allLocation);
return newData;
}
Reference
String.concat()
Im trying to send an email from google sheets and I've setup the columns to represent the subject, text and email addresses. The problem is that I need to add a hyperlink in the middle of the message and im stuck here. I can't get the paragraph to format correctly AND have the hyperlink replace a word in the middle of the sentence.
This is the code:
function AISEMAIL() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Email Addresses');
var sheet2=ss.getSheetByName('Email Fields');
var subject = sheet2.getRange(2,1).getValue();
var message = sheet2.getRange(2,2).getValue();
var calendardisplayname = sheet2.getRange(2,3).getValue();
var calendarlink = sheet2.getRange(2,4).getValue();
var formdisplayname = sheet2.getRange(2,5).getValue();
var formlink = sheet2.getRange(2,6).getValue();
message=message.replace("<calendar>",calendardisplayname).replace("<form>",formdisplayname);
var n=2;
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
let options = {
htmlBody: message
+ '' + calendardisplayname + ''
+ '' + formdisplayname + ''
}
GmailApp.sendEmail(emailAddress, subject, message,options);
}
}
Problem:
You are always trying to append the links at the end on your options. Also, it becomes redundant. What you need to do is include the links when you replace the values of <calendar> and <form>.
Code:
// Add the links on the replace
message = message
.replace("<calendar>", '' + calendardisplayname + '')
.replace("<form>", '' + formdisplayname + '');
var n = 2;
for (var i = 2; i < n + 1; i++) {
var emailAddress = sheet1.getRange(i, 1).getValue();
let options = {
htmlBody: message
}
GmailApp.sendEmail(emailAddress, subject, message, options);
}
Old Output:
New Output:
This is just a guess, but could you use HtmlService to create your html message?
function AISEMAIL() {
var ss = SpreadsheetApp.getActiveSpreadsheet()
var sheet1=ss.getSheetByName('Email Addresses');
var sheet2=ss.getSheetByName('Email Fields');
var subject = sheet2.getRange(2,1).getValue();
var message = sheet2.getRange(2,2).getValue();
var calendardisplayname = sheet2.getRange(2,3).getValue();
var calendarlink = sheet2.getRange(2,4).getValue();
var formdisplayname = sheet2.getRange(2,5).getValue();
var formlink = sheet2.getRange(2,6).getValue();
message=message.replace("<calendar>",calendardisplayname).replace("<form>",formdisplayname);
var n=2;
for (var i = 2; i < n+1 ; i++ ) {
var emailAddress = sheet1.getRange(i,1).getValue();
//ADDED htmlText VARIABLE
var htmlText;
let options = {
// ADDED HtmlService.createHtmlOutput()
htmlBody: htmlText = HtmlService.createHtmlOutput(message
+ '' + calendardisplayname + ''
+ '' + formdisplayname + '');
}
// CHANGED message TO htmlText
GmailApp.sendEmail(emailAddress, subject, htmlText,options);
}
}
REFERENCES
HtmlService
Also, if you need to create dynamic html for each email, check out these links...
createTemplateFromFile()
.evaluate()
Templated HTML
I have this piece of code
(function() {
'use strict';
// Returns current URL page.
var currentURL = window.location.protocol + "//" + window.location.host + "/" +
window.location.pathname + window.location.search
var market_table = document.getElementById("market_item_table");
var market_table_cells = document.getElementsByTagName("td");
var rowLength = market_table.rows.length;
var items = document.querySelectorAll('div[data-item-id]')
var j = 0;
items.forEach(function(getItem) {
var itemData = items[j].dataset;
var tooltip = JSON.parse(itemData.tooltip)[0][0][0];
console.log(tooltip);
j++
});
for (var i = 1; i < rowLength; i++) {
var oCells = market_table.rows.item(i).cells;
console.log("Paka nr. " + i + "Nazwa przedmiotu: " + tooltip + ' ' +
oCells[1].innerText + ' ' + oCells[2].innerText);
}
})();
Now how would I go about and get the value of variable tooltip every time it loops through the NodeList?
The bigger picture is that I want to assign the correct names to the correct items listed.
You can set up an array outside of the for loop that stores the values of the tooltips, then use it in a separate function:
var tooltips = [];
Items.forEach(function(getItem) {
var itemData = getItem.dataset;
var tooltip = JSON.parse(itemData.tooltip)[0][0][0];
console.log(tooltip);
});
for (var i=0; i < rowlength... {
// Do something with the tooltips
console.log(tooltips[i]);
}
I am trying to print out the values of the array in a google doc. I do get the correct values but it goes on printing out a number of "undefined" values. The simplest way is probably to filter out the undefined values before I print out the array.
Here is the array declaration:
var paramArr = Object.keys(e.parameter).reverse();
var tableArr = [];
for(var i = 0; i < paramArr.length - 1; i++) {
var tempArr;
var nameSelector = "Company:" + i;
var startDateSelector = "Started:" + i;
var endDateSelector = "Ended:" + i;
var referenceSelector = "Reference:" + i;
var descriptionSelector = "Description:" + i;
tempArr = [e.parameter[nameSelector] + " ",
e.parameter[startDateSelector] + " - " +
e.parameter[endDateSelector]+ "\n\n" +
e.parameter[descriptionSelector]
];
I have tried this, but it doesn't work:
tempArr = tempArr.filter(function(element){
return element !== undefined;
});
I have 2 pages. On page 1 i have two values, one is string and one is datetime.
I want to pass that value to page 2 but i have problem, the string is passed but datetime is not passed. This is my code
time = jQuery('#timepicker_7').val();
var url = "test.aspx?name=" + encodeURIComponent(lObjSeat[0].Name) + "&technology=" + encodeURIComponent(time);
window.location.href = url;
this is what's on page 2
<script type="text/javascript">
var queryString = new Array();
$(function () {
if (queryString.length == 0) {
if (window.location.search.split('?').length > 1) {
var params = window.location.search.split('?')[1].split('&');
for (var i = 0; i < params.length; i++) {
var key = params[i].split('=')[0];
var value = decodeURIComponent(params[i].split('=')[1]);
queryString[key] = value;
}
}
}
if (queryString["name"] != null) {
var data = "<u>Values from QueryString</u><br /><br />";
data += "<b>Name:</b> " + queryString["name"] + " <b>Technology:</b> " + queryString["technology"];
$("#lblData").html(data);
}
});
"name" is done. but "time" is not show. whats the problem ?