Getting current sentence using Office Javascript API - javascript

Using the Office Javascript API, I want to be able to select the current sentence in Word. By current sentence, I mean identifying where the caret position is, then iterating from that position to get the full sentence.
However, looking at the available API calls (such as getSelectedData), this does not seem possible, as there doesn't seem to be a way to get the current caret position.
I know when creating a C# project, you could use 'Microsoft.Office.Interop.Word.Range' and 'Selection.Range' to get a range, which you could use as a caret position.
Am I wrong or can you not get the caret position using the Javascript API?

Late but maybe still helpful for someone:
You can achieve this by combining getSelection() and getTextRanges([separators])on a document - this expands the current selection in both directions, until any of the characters in the list of separators is found.
Documentation for getTextRanges: https://dev.office.com/reference/add-ins/word/range
Example (typescript):
Word.run(context => {
let sentences = context.document.getSelection().getTextRanges(['\n', '.', '?'], false);
context.load(sentences);
context.sync().then(() => {
console.log(sentences.items);
}
return context.sync();
}

Related

Paperjs Group with PointText

I am trying to achieve a simple thing - I need a text within a circle (or different path shape) and treat them as a single "object". I thought that the Group object is the best solution for it. However, the paperjs behaves strange once I add a PointText into the group.
Please check this sketch.
If there is the text in the group
new Group([circle, text])
The result of clicking on the circle is only
event.item = Group #83
But if you do not add the text into the group, then the result is
event.item = Group #87
hit2.item = Group #87
So for whatever reason if there is a text in the group, then the result of hitTest is null.
Do you see, please, if there is a bug in my code or in the library itself?
Indeed, this is weird and it looks like a bug in the library itself.
Could you please post it as a new issue here: https://github.com/paperjs/paper.js/issues, we'll try to fix it as soon as possible.
As a workaround, if you know that your text is contained within the other shape, you could simply do the hit testing on the other shape directly rather than on the group:
var hit2 = circle.hitTest(event.point);
Edit
Actually, I noticed that this also work if you call the hit testing on the group directly:
var hit2 = group.hitTest(event.point);

Javascript optimizing regex function to extract sentences from a string containing a keyword

I currently have a function to take a string, break it up into sentences, then extract the sentences that contain a user defined keyword and return them:
function getSentencesWithWord(word, text) {
let sentenceArray = text.replace(/([.])\s*(?=[A-Z])/g, "$1|").split("|")
return sentenceArray.filter(sentence => sentence.includes(word))
}
Currently, this function works. However, the amount of text I need to search through with this function is quite large; it is searching through around 30 google docs that could each be up to 75 pages each, and searching for certain terms (such as the word "the") can take up to a minute for the function to complete. Is there a more optimized way to search through and extract this much text?
Edit:
Because someone asked in the comments, the word variable is just a string obtained from a text input on an HTML page and the text variable is a string obtained via this function in a Google Apps Script:
function getText(docID){
let doc = DocumentApp.openById(docID);
let textToSearch = doc.getBody().getText();
return textToSearch;
}
I have an array of all the google doc IDs I need to access and I just iterate over the array and obtain the text for each doc
The replace you've provided, replace(/([.])\s*(?=[A-Z])/g, "$1|"), didn't work for me == got the same string without change...
How about this:
("|"+text.toLowerCase().replace(/\s/g, "|")+"|").includes("|"+word.toLowerCase()+"|")
Or, this:
("|"+text.toLowerCase().split(" ").join("|")+"|").includes("|"+word.toLowerCase()+"|")
Was unable to find a faster method of achieving this with Google Apps Script so I just went back to Node.js and ended up using the Google Drive API to download the files as .txt files, which are much smaller and so I can search through them much faster now

React Cursor jumping when formatting textfield

I know there are a lot of topics on this, and I've done my best reading and implementing their solutions, but I just cant get it to work properly
Im trying to format the phone number so that it displays in a certain format.
I created a demo of how it works in my app.
https://codesandbox.io/s/dank-leaf-94s3w?file=/src/App.js
Edit: sorry to describe the issue a bit more.
everything works fine when you are just typing out a # front to back
but when I try to modify the middle numbers the cursor will jump to the end after each input.
Thanks
Hi so the problem is that you pass a new value and that will be just like inputing the whole value. And this will cause the cursor to move to the end.
You can overcome it by requesting a window animation frame and move the cursor back to where it was:
const caret = target.selectionStart;
const element = target;
window.requestAnimationFrame(() => {
element.selectionStart = caret;
element.selectionEnd = caret;
});
Here is a copy of your sandbox.
You are passing the ref into the wrong element. In #material-ui/core/TextField you must pass the ref to the inputProps instead of direct ref.
Just change the:
ref={input}
to:
inputProps={{ ref: input }}
There are other issues in your code such as not parsing the value to the normal string state but the issue for jumping was for ref

Scan dom/webpage after certain pattern and get domtag

I write a small chrome extension which includes adding buttons add specific positions.
These positions are mostly random and can't be determined with normal css/jQuery selectors.
I need to scan the whole page for a certain text pattern (regex).
After I found matches I need to get the dom tag where the text is in.
I tried parsing the whole source with body.innerHtml but I cant get the tag obj afterwards.
Any ideas on how to accomplish such a task are highly appreciated!
Sounds like you could use :contains() for this.
$(":contains('Your Text')")
For finding text using a regular expression use .filter()
var regex = new RegExp("Your Text");
$("*").filter(function () {
return regex.test($(this).text());
});

Remove line of hyphens

I'm trying to remove redundant dashed lines from a client's blog site because its erratic lengths are butting heads with the responsive design. Example: ---------------------------------
I created a jquery plug-in to remove individual lines from one post:
$('p:contains(——————————————————————————————-),
p:contains(———————————————————————–),
p:contains(————————————————————————————-),
p:contains(———————————————————————————),
p:contains(——————————————————————–),
p:contains(————————————————————-),
p:contains(————————————————————————————————),
p:contains(—————————————————————————),
p:contains(————————————————————),
p:contains(———————————————————————————–),
p:contains(——————————————————————————-)').each(function() {
$(this).remove();
});
But even this method is redundant lol. I tried rewriting like so using regex:
$('p:contains(----)').each(function(text) {
var hyphen = text.replace(/\u00AD/g,'');
return hyphen;
});
It didn't work and I've since been stuck on it for an hour. If anyone could give me a push in the right direction I would greatly appreciate it. Thanks!
Giving the jQuery .text() method a function is the way to perform a direct replacement of an element's text. The function receives the old text as its second argument, and whatever it returns will be used as the replacement.
$("p:contains(---)").text(function(i, text) {
return text.replace(/-{3,}/g, '');
});

Categories