Office.js wildcard search with special characters crashes Word Online but not installed MSWord - search

Wildcard searches such as the example detailed in the following article crashes Word Online, whilst working as expected for the installed version of MSWord
https://learn.microsoft.com/en-us/office/dev/add-ins/word/search-option-guidance
I'm searching for the following text:
'Payment of invoices by Sponsor shall be made within thirty (30'
The text may or may not be broken across line/section breaks. Therefore I'm performing a wildcard search as follows:
'Payment?of?invoices?by?Sponsor?shall?be?made?within?thirty?(30'
This causes Word-Online to crash the browser.
Interestingly the following works in both Word Online and MSWord:
'Payment of invoices by Sponsor shall be made within thirty (30) days'
'Payment?of?invoices?by?Sponsor?shall?be?made?within?thirty?(30)?days'
There appears to be a problem with un-closed parenthesis in the wildcard expression.
Following the article above, it suggests that the parenthesis should be escaped with square brackets. The following worked in MSWord but crashed Word-Online:
'Payment?of?invoices?by?Sponsor?shall?be?made?within?thirty?[(]30'
Even stranger is that the square brackets escapement only works if the last character is being escaped.
This works in both MSWord and Word-Online:
'include?intangible?things?[(]'
whereas this only works in MSWord and crashes Word-Online
include?intangible?things?[(]e
The example pattern in the article above ' [ * ] * ' crashes Word Online, whilst working as expected in MSWord.
input = 'include?intangible?things?[(]e';
const { Word } = window;
Word.run(context => {
const { body } = context.document;
const searchResults = body.search(input, {
matchCase: false,
ignoreSpace: true,
ignorePunct: true,
matchWildcards: true
});
context.load(searchResults);
return context
.sync()
.then(() => {
})
.catch(error => {
});
});
Both MSWord and Word Online should behave the same when both are executed through the Office.js API.
This looks like a bug in Word Online. I would speculate that it's probably something to do with url encoding.
Either that, or I'm not encoding my input pattern correctly when the target is Word Online. In which case the article referenced above needs updating to detail what the Word Online 'escapes' are for the special characters.
Curiously the escaping [ ] brackets are themselves special characters.
Word Online crashing

Not a perfect answer this, but it solves the Word Online crashing issue and the un-closed parenthesis problem in one hit. I'm replacing all the special characters in my search string with '?' characters. This might result in a misidentified piece of text, but the likelihood is low enough for this to not be a problem for what I'm trying to achieve.
function replaceSpecialCharacters(input) {
let modifiedInput = '';
const specialCharacters = ['[', ']', '(', ')', '{', '}', '*', '?', '<', '>', '!', '#'];
[...input].forEach(c => {
if (specialCharacters.includes(c)) {
modifiedInput = `${modifiedInput}?`;
} else {
modifiedInput = `${modifiedInput}${c}`;
}
});
return modifiedInput;
}

Related

Remove trailing spaces in string in google apps script

I am grouping by "Id" and get the sum of "Total_Weight" in google apps script. This is the result of that computation.
res_1 output:
[ { Id: '400 ', Total_Weight: 484308 },
{ Id: '500W', Total_Weight: 13232 } ]
After this, I have a if-else clause that loops over the "Id" in above array and does some computation.
res_1.forEach((r2,i2)=>{
if (r2['Id']=="400") {
var cost = (r2['Total_Weight']/1000)*cost
NewArray.push([r2['Id'], cost]);
}
else if (r2['Id']=="400W") {
var cost = (r2['Total_Weight']/1000)*cost
NewArray.push([r2['Id'], cost ]);
}
}
My challenge is in "res_1" the first Id is "400 " (400 followed by a space). Hence, when it comes to the for loop, it does not go into the first if clause. I tried with replacing spaces but that doesnt work as well.
Are there any ways that this could be resolved? Any leads would be great.
Try using the .replace call on the res_1 output like so:
var res_1_trim = res_1.replace(/\s/g, "")
res_1_trim.forEach((r2,i2)=>{
if (r2['Id']=="400") {
var cost = (r2['Total_Weight']/1000)*cost
NewArray.push([r2['Id'], cost]);
}
else if (r2['Id']=="400W") {
var cost = (r2['Total_Weight']/1000)*cost
NewArray.push([r2['Id'], cost ]);
}
}
\s is a solution to find whitespace and the 'g' provides a match for instances of whitespace.(.replace documentation)

Bot won't react after seeing a word contained in a word list

I am trying to make my bot react to every message containing words like hey or hi, but when I send a message containing one of those, it does nothing, here is my code:
const words = [
'hey',
'Hey'
]
client.on('message', () => {
if (message.content.includes(words)) return message.react("👋")
})
There are no errors in the console either.
Issue
message.content.includes(words)
is equivalent to
message.content.includes(words.toString())
which actually checks if the string joining the words array by a comma exists in message.content (not what you want). In your case, it does the following:
message.content.includes('hey,Hey') // words = ['hey', 'Hey']
Array.prototype.toString returns a string joining the array elements by a comma.
Solution
You can use Array.prototype.some to check if any of the words in the words array is present in the message content. And you don't have to add the capitalized word forms to words. It's enough to only add lowercase words and use String.prototype.toLowerCase to lower case the message content before checking if any of the entries in words exists in the content.
const words = [
'hey',
// ...
]
const content = message.content.toLowerCase()
const shouldReact = words.some((word) => content.includes(word))
if (shouldReact) {
// ...
}

Selecting (and deleting) entire function with definition (INCLUDING whitespace) in Vim

I recently switched to using Vim (with VSCode) as my editor.
I'm trying to delete a function with it's definition in JavaScript. I looked on google and here on StackOverflow and found this question. Unfortunately the answers for this question only work for functions without white space.
Here is how my function looks:
const useBattery = () => {
const [battery, setBattery] = useState({ level: 0, charging: false });
const handleChange = ({ target: { level, charging } }) => setBattery({ level, charging });
useEffect(() => {
let battery;
navigator.getBattery().then(bat => {
battery = bat;
battery.addEventListener("levelchange", handleChange);
battery.addEventListener("chargingchange", handleChange);
handleChange({ target: battery });
});
return () => {
battery.removeEventListener("levelchange", handleChange);
battery.removeEventListener("chargingchange", handleChange);
};
}, []);
return battery;
};
I tried several approaches, the best one was da{ when my cursor is within the function. This motion will delete the function body, but not the definition.
Is there any way to delete the function and the definition in one motion using Vim, if there is white space in the function?
From inside the function, as you say da{ deletes only the braces and its content, without the preceding declaration or the following semicolon. However... if we switch to linewise...?
There is a semi-hidden section a bit under :help exclusive-linewise with bold heading but no tag to jump to: "FORCING A MOTION TO BE LINEWISE, CHARACTERWISE OR BLOCKWISE", saying that we can switch to a non-default selection by using v (characterwise), V (linewise) or Ctrl-V (blockwise) immediately after the operator. So...
dVa{
As mentioned in the post you linked to, d]] when the cursor is placed at the beginning of the function definition will delete the whole function.

Internet Explorer truncating flashVars containing JSON

This only happens in IE.
I'm using swfobject and loading the flash vars as such
var flashVars = {
myVar:'{"url":"http://google.com/", "id":"9999"}',
};
var params = {
allowFullScreen:"true",
wmode:"transparent",
allowScriptAccess:'always'
};
swfobject.embedSWF("mySwf.swf", "mySwf", "512", "318", "10.0.0", "./js/swfobject/expressInstall.swf", flashVars, params);
Everything works perfectly in all browser but IE. I checked myVar and it comes into the swf as { and that's it. I know it's dying at the '. I've tried putting a \ infront, then tried \\ and kept adding one slash until I got to \\\\\\\\. I even inverted all the slashes and tried the same ritual. Nothing.
I can get the string to finally come through, with inverted quotes and using double slashes, but then my JSON parser gets mad about there being slashes in my string.
Here's an example of what works, but of what is invalid JSON:
"{\\'url\\':\\'http://google.com/\\', \\'id\\':\\'9999\\'}"
Yep IE treats flashVars differently to all the other major browsers, I believe you need to make use of the JavaScript encodeURIComponent method which will escape all reserved characters from your String, eg:
// Removing all reserved characters from the flashVar value.
var flashVars = {
myVar: encodeURIComponent('{"url":"http://google.com/", "id":"9999"}'),
};
If you are passing multiple values in the flashVars then you could iterate through them and encode all chars in a single pass:
var flashVars = {
myVar: '{"url":"http://google.com/", "id":"9999"}',
anotherVar: 42
};
// Escape all values contained in the flashVars object.
for (key in flashVars) {
if (flashVars.hasOwnProperty(key)) {
flashVars[key] = encodeURIComponent(flashVars[key]);
}
}
As #dgmdan and #bcmoney suggested, it would probably make your code easier to read if you made use of JSON.stringify - however, you need to bear in mind that IE8 and below do not have a native JSON object, so you will need to include Crockford's JS Library in your HTML page.
// Making use of a JSON library.
var flashVars = {
myVar: encodeURIComponent(JSON.stringify({ url: "http://google.com/", id: "9999"})),
};
Also, it's worth bearing in mind that flashVars are limited to ~64k; so if you are planning to pass a lot of data, it might be better to use an ExternalInterface call to pull them from the JavaScript instead.
Try this to replace your first 3 lines:
var subVars = { url: "http://google.com/", id: "9999" };
var flashVars = { myVar: JSON.stringify(subVars) };

Sharepoint > Which characters are *not* allowed in a list?

I've tried looking this up and haven't come up with the answer I'm looking for; I've found what cannot be included in filenames, folder names, and site names... but nothing on actual fields in a list.
I noticed that the percent symbol (%) is one that's not allowed in files/sites/folders. But it also doesn't populate when I try to pro grammatically add the fields to the list. I am doing this by using a small C# application that sends the data via Sharepoint 2010's built-in web services. I can manually enter the character, but it messes up each field in the row if I try it through code.
I've tried some of the escape characters that I've found via Google (_x26), but these don't seem to work either. Has anyone else had an issue with this? If these characters are allowed, how can I escape them when sending the data through a web service call?
Thanks in advance!
Justin
Any characters that aren't allowed when you enter a field name get encoded in the internal name. The format is a little different to what you show - try "_x0026_".
I usually avoid issues with weird internal names by creating the field with no spaces or special characters in the name, then renaming it. When you rename a field, only the display name changes and you keep the simple internal name.
Characters not allowed in SharePoint file name:
~, #, %, & , *, {, }, \, :, <, >, ?, /, |, "
Pasted from http://chrisbarba.com/2011/01/27/sharepoint-filename-special-characters-not-allowed/
Am I saying something weird when I state that there usually is a reason for certain characters not being allowed. I don't know which or why, but there probably is a reason.
Since you control which fields need to be there you can also dictate their (internal) names. I'd say follow best practice and name your fields using Camel case. And because you created them, you can just map the fields to the according fields in your data source.
As a follow on to #Elisa's answer, here's some JavaScript / TypeScript code that helps to prevent users from uploading files into SharePoint with invalid characters in the file name implemented on Nintex forms.
Here's the gist of the JavaScript version (note you'll have to obviously adapt for your own needs since this was designed for Nintex) :
//------------------------------------------------------------------------
//JavaScript Version:
//Code from http://cc.davelozinski.com/tips-techniques/nintex-form-tips-techniques/javascript-typescript-for-nintex-forms-to-validate-file-names
//------------------------------------------------------------------------
function validateAttachmentNames(eventObject) {
var textbox$ = NWF$(this);
var attachrowid = this.id.substring(10, 47);
var fileUploadid = attachrowid;
var index = attachrowid.substring(36);
//console.log('index:' + index);
//console.log('attachrowid:' + attachrowid);
//console.log('fileUploadid:' + fileUploadid);
if (index == '') {
attachrowid += '0';
}
var fileName = NWF.FormFiller.Attachments.TrimWhiteSpaces(textbox$.val().replace(/^.*[\\\/]/, ''));
var match = (new RegExp('[~#%\&{}+\|]|\\.\\.|^\\.|\\.$')).test(fileName);
if (match) {
isValid = false;
setTimeout(function () {
NWF$("tr[id^='attachRow']").each(function () {
var arrParts = (NWF$(this).find(".ms-addnew")[0]).href.split('"');
var fileName = arrParts[5];
var attachRow = arrParts[1];
var fileUpload = arrParts[3];
var match = (new RegExp('[~#%\&{}+\|]|\\.\\.|^\\.|\\.$')).test(fileName);
if (match) {
console.log(fileName);
NWF.FormFiller.Attachments.RemoveLocal(attachRow, fileUpload, fileName);
alert('Invalid file: ' + fileName + ' You cannot attach files with the following characters ~ # % & * { } \ : < > ? / + | \n\nThe file has been removed.');
}
});
}, 500);
}
}

Resources