How do you make the console print true when the variable "message" contains an element from each array in order: "greetings" then "colour" then "names", but any words before "greetings" and after "colour" still prints true.
If the elements are out of order it should print false.
If there are words in-between the elements it should print false.
Check examples below current code.
Current Code:
var message = '';
var greetings = 'hi' || 'hello' || 'hey';
var colour = 'red' || 'green' || 'blue';
var names = 'john' || 'evan' || 'etienne';
if (message.includes(greetings + ' ' + colour + ' ' + names)) {
console.log(true);
} else {
console.log(false);
}
Examples of what it should print when message:
message = 'hi green evan' //true
message = 'lol hey blue john' //true
message = 'hello green etienne! xD' //true
message = 'evan green hi' //false because wrong element order
message = 'hi very green evan' //false because word in-between elements
You can use regex for this.
var message = "hello green etienne! xD";
var greetings = ["hi", "hello", "hey"].join("|");
var colors = ["red", "green", "blue"].join("|");
var names = ["john", "evan", "etienne"].join("|");
var reg = new RegExp("^.*(" + greetings + ")\\s+.*(" + colors + ")\\s+(" + names + ").*$");
console.log(reg.test(message));
var message = 'hi green evan';
var greetings = ["hi","hello","hey"];
var colour = ['red','green','blue'];
var names = ['john','evan','etienne'];
var contained = false;
greetings.some(x =>{
colour.some(z=>{
names.some(e=>{
if(message.includes(x + ' ' + z + ' ' + e)){
contained = true;
alert(true);
}
});
});
});
if(!contained)
alert(false);
You could do it like this. Probably not the best way to do it but the first thing that came to my mind
Related
I am trying to use textFinder to return the data from recordsSheet; I expect cslData[0] to return the employee ID number; however, it is returning the character in the 0 position of the string value.
function textFinder(findID){
var recordData;
var findRecord = recordsSheet.createTextFinder(findID);
var foundRange = findRecord.findNext();
var fRng = recordsSheet.getRange(foundRange.getRow(),1,1,9)
while(null != foundRange){
recordData.push(fRng.getValues());
foundRange = findRecord.findNext();
}
return(recordData);
}
var ss = SpreadsheetApp.getActiveSpreadsheet();
var recordsSheet = ss.getSheetByName("Active Records");
function onFormSubmit(e) {
var eRng = e.range;
var eRow = eRng.getRow();
var colA = ss.getRange('A' + eRow).getValue()
//The second value of the form response (e) is in Column A
Logger.log("Call txt finder")
var cslData = textFinder(colA).toString().split(",").join();
Logger.log("cslData: " + cslData)
Logger.log("cslData[0]: " + cslData[0])
Logger.log("cslData[1]: " + cslData[1])
Logger.log("cslData[2]: " + cslData[2])
Logger.log("cslData[0][0]: " + cslData[0][0])
}
I was expecting cslData[0] to return "100###5"
Modification points:
In your script, eRow is not declared. And, e is not used. Please be careful about this. In this modification, it supposes that eRow is declared elsewhere.
In your script, var recordData; is not an array. So, I think that an error occurs at recordData.push(fRng.getValues());. So, I'm worried that your showing script might be different from your tested script.
If var recordData; is var recordData; = [], about your current issue, in your script, the array of recordData is converted to the string by var cslData = textFinder(colA).toString().split(",").join();. By this, cslData[0] returns the top character. I thought that this is the reason for your issue.
And, if var recordData; is var recordData; = [], recordData is 3 dimensional array.
In your situation, I thought that findAll might be useful.
When these points are reflected in your script, how about the following modification?
Modified script:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var recordsSheet = ss.getSheetByName("Active Records");
function textFinder(findID) {
return recordsSheet
.createTextFinder(findID)
.findAll()
.map(r => recordsSheet.getRange(r.getRow(), 1, 1, 9).getValues()[0]);
}
function onFormSubmit(e) {
var colA = ss.getRange('A' + eRow).getValue();
var cslData = textFinder(colA);
Logger.log("cslData: " + cslData);
if (cslData.length > 0) Logger.log("cslData[0]: " + cslData[0][0]);
}
or, I think that you can also the following script.
function onFormSubmit() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var recordsSheet = ss.getSheetByName("Active Records");
var eRow = ###; // Please set this.
var findID = ss.getRange('A' + eRow).getValue();
var cslData = recordsSheet.createTextFinder(findID).findAll().map(r => recordsSheet.getRange(r.getRow(), 1, 1, 9).getValues()[0]);
Logger.log("cslData: " + cslData)
if (cslData.length > 0) Logger.log("cslData[0]: " + cslData[0][0])
}
Reference:
findAll()
My code below opens 4 text boxes, 1 asking a YES/NO question and 3 asking OK/CANCEL questions. Every one of them accepts text, but I want the first question to only accept buttons YES and NO without a text box. Also, if the answer to the first question is NO, I want it to skip the second question and go straight to the third question.
Questions are as follows:
Did we do work for this client today/yesterday? [YES/NO]
What did we help this client with today? [textbox][OK/CANCEL] ***skip if previous answer is no
When should we follow up with this client next? (MM/DD/YY) [textbox][OK/CANCEL]
Next Follow Up Activity? [textbox][OK/CANCEL]
function TextBox() {
var ui = SpreadsheetApp.getUi();
const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS');
sheet.sort(9)
var today = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yy")
var ui = SpreadsheetApp.getUi();
var valuesToCopy = sheet.getRange("C5:J5").getDisplayValues().map(r => r.join(' \n '));
var ar2 = [
{
message: valuesToCopy + "\n\n Did we do work for this client today/yesterday? (YES/NO)",
range: "G5"
}
];
var ar3 = [
{ // promptUPDATE3
message: valuesToCopy + "\n\n What did we help this client with today?",
range: "H5"
},
];
var ar = [
// { // promptUPDATE3
// message: valuesToCopy + "\n\n What did we help this client with today?",
// range: "PREVIOUS"
// },
{ // promptUPDATE
message: valuesToCopy + "\n\n When should we follow up with this client next? (MM/DD/YY)",
range: "I5"
},
{ // promptUPDATE2
message: valuesToCopy + "\n\n Next Follow Up Activity?",
range: "J5"
}
];
ar2.forEach(({message, range }) => {
var res = ui.prompt(message, ui.ButtonSet.YES_NO);
if (res.getSelectedButton() == ui.Button.YES) {
sheet.getRange("G5").setValue(today);
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
})
ar3.forEach(({ message, range }) => {
var res = ui.prompt(message, ui.ButtonSet.OK_CANCEL);
var lastRowSourceData = sheet.getRange("H5").getValue();
var lastActivity = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS').getRange("H5").getValue();
if (res.getSelectedButton() == ui.Button.OK) {
sheet.getRange(range).setValue(lastActivity+" | "+res.getResponseText());
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
})
ar.forEach(({ message, range }) => {
var res = ui.prompt(message, ui.ButtonSet.OK_CANCEL);
if (res.getSelectedButton() == ui.Button.OK) {
sheet.getRange(range).setValue(res.getResponseText());
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
})
}
SUGGESTION [SCRIPT UPDATED]
Perhaps you can try this tweaked script below. This will use the alert Class Ui method for the first question & an if condition to skip the order of questions if the user selects NO.
In my understanding, here is the flow you want to achieve:
The first question should not have a Textbox but only give the user to select YES or NO buttons.
If user selects NO on the first question, route the user to the third question instead.
Otherwise, the user will be prompted with the questions in order.
NOTE: This sample script will only process the first client on your sheet just like on your actual script. If I have misunderstood something Or if there's anything else missing, feel free to let me know.
Sample Tweaked Script
function TextBox() {
var ui = SpreadsheetApp.getUi();
var today = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yy");
const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('HOT & WARM CLIENTS');
sheet.sort(9);
var sheetData = sheet.getRange("C5:J5").getDisplayValues();
var questions = [['\n\n Did we do work for this client today/yesterday? (YES/NO)', "G"],
['\n\n What did we help this client with today?', "H"],
['\n\n When should we follow up with this client next? (MM/DD/YY)', "I"],
['\n\n Next Follow Up Activity?', "J"]];
var row = "5"; //ONLY process current client on 5th row
var clientDetails = sheetData[0].join('\n');
/**First question */
var q1 = ui.alert(clientDetails + questions[0][0], ui.ButtonSet.YES_NO)
if (q1 == ui.Button.YES) {
sheet.getRange(questions[0][1] + row).setValue(today);
/**End of the First question */
/**Second question */
var q2 = ui.prompt(clientDetails + questions[1][0], ui.ButtonSet.OK_CANCEL);
var lastActivity = sheet.getRange(questions[1][1] + row).getValue();
q2.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[1][1] + row).setValue(lastActivity + " | " + q2.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
/**End of the Second question*/
/**Third question */
var q3 = ui.prompt(clientDetails + questions[2][0], ui.ButtonSet.OK_CANCEL);
q3.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[2][1] + row).setValue(q3.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
/**End of the Third question*/
/**Fourth question */
var q4 = ui.prompt(clientDetails + questions[3][0], ui.ButtonSet.OK_CANCEL);
q4.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[3][1] + row).setValue(q4.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
/**End of the Fourth question*/
} else {
/**Skip to the third question if 'NO' was selected on the first question*/
var q3 = ui.prompt(clientDetails + questions[2][0], ui.ButtonSet.OK_CANCEL);
q3.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[2][1] + row).setValue(q3.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
/**End to fourth question*/
var q4 = ui.prompt(clientDetails + questions[3][0], ui.ButtonSet.OK_CANCEL);
q4.getSelectedButton() == ui.Button.OK ? sheet.getRange(questions[3][1] + row).setValue(q4.getResponseText()) : console.log('The user clicked "No" or the dialog\'s close button.');
}
}
Demonstration
- Quick Test
- E.g. If the user selects NO on the first question & then gets skipped to the third question
References
alert(prompt, buttons)
I don't think putting comments inside you objects is a good idea
Try it this way:
function lfunko() {
var ui = SpreadsheetApp.getUi();
const sheet = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS');
sheet.sort(9)
var today = Utilities.formatDate(new Date(), "GMT-6", "MM/dd/yy")
var ui = SpreadsheetApp.getUi();
var valuesToCopy = sheet.getRange("C5:J5").getDisplayValues().flat().map(r => r.join(' \n '));
var ar2 = [{ message: valuesToCopy + "\n\n Did we do work for this client today/yesterday? (YES/NO)", range: "G5" }];
var ar3 = [{ message: valuesToCopy + "\n\n What did we help this client with today?", range: "H5" }];
var ar = [{ message: valuesToCopy + "\n\n What did we help this client with today?", range: "PREVIOUS" }, { message: valuesToCopy + "\n\n When should we follow up with this client next? (MM/DD/YY)", range: "I5" }, { message: valuesToCopy + "\n\n Next Follow Up Activity?", range: "J5" }];
ar2.forEach(obj => {
var res = ui.prompt(obj.message, ui.ButtonSet.YES_NO);
if (res.getSelectedButton() == ui.Button.YES) {
sheet.getRange("G5").setValue(today);
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
});
ar3.forEach(obj => {
var res = ui.prompt(obj.message, ui.ButtonSet.OK_CANCEL);
var lastRowSourceData = sheet.getRange("H5").getValue();
var lastActivity = SpreadsheetApp.openById('1JZ-v5n5m0_lyaoLgsHDAa78AtYNP5AsUDo2NRpJbwG4').getSheetByName('HOT & WARM CLIENTS').getRange("H5").getValue();
if (res.getSelectedButton() == ui.Button.OK) {
sheet.getRange(range).setValue(lastActivity + " | " + res.getResponseText());
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
});
ar.forEach((obj) => {
var res = ui.prompt(obj.message, ui.ButtonSet.OK_CANCEL);
if (res.getSelectedButton() == ui.Button.OK) {
sheet.getRange(obj.range).setValue(res.getResponseText());
} else {
Logger.log('The user clicked "No" or the dialog\'s close button.');
}
});
}
And the UI does not contain any drop down selections. You would have to build one with html.
I am developing an application with local notifications with values coming from the database. However, it is repeating the notification with the same value countless times until changing to another.
Example:
1st - "The invoice of house #1 will expire"
2nd - "The invoice of house #1 will expire"
3rd - "The invoice of house #2 will expire"
Any idea what that might be and how to fix it?
calculateDif(idHouse, dateBill) {
let billDate= moment(dateBill);
var MyDate = new Date();
var MyDateString;
MyDateString = MyDate.getFullYear() + '-' + ('0' + (MyDate.getMonth()+1)).slice(-2)
+ '-' + ('0' + MyDate.getDate()).slice(-2);
let warningDate= billDate.diff(MyDateString, 'days');
if (warningDate <= 5) {
this.localNotifications.schedule({
id: 1,
text: 'The invoice of house ' idHouse + ' will expire',
sound: null,
data: { secret: 1 }
});
}
}
I think the problem is in the function that execute calculateDif();
You can also create an array of your articles that you have already notified, for example notifiedHouses = []
and check if the id is already notified using .some
calculateDif(idHouse, dateBill) {
let billDate= moment(dateBill);
var MyDate = new Date();
var MyDateString;
MyDateString = MyDate.getFullYear() + '-' + ('0' + (MyDate.getMonth()+1)).slice(-2)
+ '-' + ('0' + MyDate.getDate()).slice(-2);
let warningDate= billDate.diff(MyDateString, 'days');
if (warningDate <= 5 && !this.notifiedHouses.some( item => item.idHouse === idHouse )) {
this.localNotifications.schedule({
id: 1,
text: 'The invoice of house ' idHouse + ' will expire',
sound: null,
data: { secret: 1 }
});
const house = {idHouse: idHouse}
this.notifiedHouses.push(house);
}
}
Could anyone point me in the correct direction?
I searched plenty... I had no luck with mapping... or "find" nor extracting the data I need.
My code:
function checkMeetCode(meetCode, index, searchDate) {
var ss=SpreadsheetApp.getActive().getSheetByName('AsistenciaMeet');
var colB = ss.getRange("D3:D").getValues();
var filasLlenasLengthB = colB.filter(String).length; //
var userKey = 'all';
var applicationName = 'meet';
var emailAddress = colB[index];
Logger.log("Index / Alumno: "+index+" / " + emailAddress);
var optionalArgs = {
event_name: "call_ended",
endTime: searchDate,
startTime: fechaRestada(searchDate),
filters: "identifier==" + emailAddress + ",meeting_code==" + meetCode
};
var response = AdminReports.Activities.list(userKey, applicationName, optionalArgs)
Logger.log("RESPONSE: "+response);
var actividad = response.items;
if (actividad == null) {
// do nothing
}
else {
**// HERE IS WHERE I AM TRYING TO FIND / EXTRACT INFORMATION FROM "response"
// READ BELOW PLEASE.**
}
}
NEED HELP WITH:
I want to FIND/EXTRACT the intValue of "duration_seconds":
The results from:
Logger.log("RESPONSE: "+response);
RESPONSE: {"kind":"admin#reports#activities","etag":"\"JDMC8884sebSctZ17CIssbQ/IhilrSKVziEhoZ7URUpQ-NrztHY\"","items":[{"events":[{"parameters":[{"name":"video_send_seconds","intValue":"1829"},{"name":"screencast_recv_packet_loss_mean","intValue":"0"},{"name":"identifier_type","value":"email_address"},{"name":"video_send_packet_loss_max","intValue":"0"},{"name":"endpoint_id","value":"meet_android_4154513448557872"},{"name":"video_recv_long_side_median_pixels","intValue":"320"},{"name":"calendar_event_id","value":"44jr4vu3qo75q6bvkknq_20200421T213000Z"},{"name":"video_send_fps_mean","intValue":"29"},{"name":"video_recv_short_side_median_pixels","intValue":"180"},{"name":"network_estimated_download_kbps_mean","intValue":"351"},{"name":"duration_seconds","intValue":"1830"},{"name":"video_send_bitrate_kbps_mean","intValue":"762"},{"name":"network_recv_jitter_msec_max","intValue":"130"},{"name":"ip_address","value":"186.59.21.55"},{"name":"audio_send_seconds","intValue":"1829"},{"name":"screencast_recv_packet_loss_max","intValue":"0"},{"name":"video_recv_seconds","intValue":"1818"},{"name":"network_rtt_msec_mean","intValue":"36"},{"name":"video_send_long_side_median_pixels","intValue":"640"},{"name":"screencast_recv_seconds","intValue":"1829"},{"name":"product_type","value":"meet"},{"name":"video_recv_packet_loss_max","intValue":"0"},{"name":"is_external","boolValue":false}],"name":"call_ended","type":"call"}] ...
OK, after roughly 8 hours... I was able to make it work.
var insideParameters = response["items"][0]["events"][0]["parameters"];
Logger.log(insideParameters.length);
for (var i = 30; i<insideParameters.length;i++){ // its always located above i = 30...
if(insideParameters[i].name === "duration_seconds"){
var duration = insideParameters[i].intValue;
Logger.log(duration);
}
}
So im trying to automaically retrieve a random entry from an array listed in a seperate node.js file
function code
var replies = require('./replies');
function followed(event) {
var name = event.source.name;
var flname = event.source.screen_name;
var myArray = [replies];
var item = myArray[(Math.random()*myArray.length)|0];
var followtweet = '#' + flname + item;
if(flname != "UserName") {
tweetIt(followtweet);
console.log('\n#' + flname + ' Followed You');
console.log('\nYou Tweeted:\n ' + followtweet);
} else {
console.log(' Interferance From Another Bot! \n');
}
}
External Array
module.exports = {
followedone: 'one',
followedtwo: 'two',
followedthre: 'three',
replyone: ' one',
replytwo: ' two',
replythre: ' three',
}
When I Run This and the function runs i get this
#LandscapesLucid Followed You
You Tweeted:
#LandscapesLucid[object Object]
Waiting For The Next Action
I'm not sure why its only showing the [object Object] instead of either one two or three like in the array
Can you try to change from this
var item = myArray[(Math.random()*myArray.length)|0];
to this?
var item = myArray[Math.floor(Math.random()*items.length)];