Find and Replace Header/Footer text Office JS - ms-word

I'm having trouble with office js and processing a list of items with lookup codes and replacement values for the header and footer. I've got the body working just not the header/footer. I'm getting this error:0x800a139e - JavaScript runtime error: The property 'items' is not available. Before reading the property's value, call the load method on the containing object and call "context.sync()" on the associated request context. As you can see I do call load and sync before trying to access the results.
function mergeHeader(documentFieldKeys) {
if (documentFieldKeys.length > 0)
Word.run(function(context) {
var key = documentFieldKeys.shift();
var mySections = context.document.sections;
context.load(mySections, 'body/style');
return context.sync().then(function() {
for (var i = 0; i < mySections.items.length; i++ ) {
findAndReplace(key, context, mySections.items[i].getHeader("primary"));
}
return context.sync().then(function() {
return mergeHeader(documentFieldKeys);
})
.then(context.sync);
});
});
}
function findAndReplace(key, context, body) {
var results = body.search(key.Code, { matchWholeWord: false, matchCase: false });
context.load(results);
return context.sync().then(function() {
if (results.items.length > 0 && key.Value === "") {
missingFields.push(key.Description);
} else {
for (var i = 0; i < results.items.length; i++) {
results.items[i].insertText(key.Value, "replace");
}
}
})
.then(context.sync);
}
Any help would be appreciated.

Add
context.load(mySections, 'items');
or
mySections.load('items');

Related

Word Addin is getting crashed when trying to delete a row which contains a content control in a table

I'm developing a Word Add-in (Word API + Office.js) where i am working with content controls, I am trying to read the table content inside a content control where I need remove the empty rows
Sample: I have this table inside a content control I have to remove the blank rows
i am able to achieve this functionality with this code, but if the table contains a content control which is blank then when i try to delete that row the addin itself is getting crashed.
function checktable(element) {
Word.run(function (context) {
// Queue a command to get the current selection and then
// create a proxy range object with the results.
var contentControl = context.document.contentControls.getByTag('control1').getFirst();
var table = contentControl.tables.getFirst();
context.load(contentControl, 'tables');
table.load('values');
return context.sync()
.then(function () {
// Get the longest word from the selection.
if (contentControl.tables.items.length === 0) {
document.getElementById('lblstatus').innerText += "No Tables found";
}
else {
document.getElementById('lblstatus').innerText += " Tables found";
var Tablevaules = table.values;
for (var i = 0, len = Tablevaules.length; i < len; i++)
{
var nullcheck = "";
var inner=Tablevaules[i];
// inner loop applies to sub-arrays
for (var j = 0, len2 = inner.length; j < len2; j++) {
// accesses each element of each sub-array in turn
if (inner[j] == "") {
if (nullcheck != "False") {
nullcheck = "True";
}
}
else {
nullcheck = "False";
}
}
if (nullcheck == "True") {
table.deleteRows(i);
}
}
}
})
.then(context.sync)
.then(function () {
// Queue a command to highlight the search results.
document.getElementById('lblstatus').innerText += element + ":" + "Successs";
});
})
.catch(errorHandler);
}
Please let me know whether i am missing something or its a known bug!!

Exception trying to get parentContentControlOrNullObject property

I´m trying to get the Title and the order of all the content controls in a Word document. The following code works:
function readContentControlsTitle() {
Word.run(function (context) {
var myDocParagraphs = context.document.body.paragraphs;
context.load(myDocParagraphs, 'text, outlineLevel');
return context.sync().then(function () {
for (var i = 0; i < myDocParagraphs.items.length; i++) {
var parContentControl = myDocParagraphs.items[i].parentContentControlOrNullObject;
context.load(parContentControl, 'title');
paragraphContentControls.push(parContentControl);
}
return context.sync().then(function () {
for (var iCount = 0; iCount < paragraphContentControls.length; iCount++) {
if (paragraphContentControls[iCount].title != null) {
// Some stuff with paragraphContentControls[iCount].title
}
}
return context.sync();
})
})
}).catch(function (error) {
if (error instanceof OfficeExtension.Error) {
console.log("Debug info: " + JSON.stringify(error.debugInfo));
}
});
}
The problem arise when I insert a table of contents in the document. In that case, I always get an exception in the second context.sync. If I remove the table of contents, it works again. The output of the exception is:
Debug info: {"code":"GeneralException","message":"GeneralException","errorLocation":"Paragraph.parentContentControlOrNullObject"}
My Word version: 16.0.7927.1020
Thank you for discovering the issue. Yes it is a bug. It is fixed now and should be released next month. Before the fix, you can put parentContentControlOrNullObject in a try/catch. Thanks.

how to collect data from user with the facebook messenger bot api in node js

I am building a messenger bot in node. I want it to collect user input data and have a conversation or ask questions, but the code I have doesn't work. the part that does not work is it only continues to the next else if block if i type the same code. and second the array is not capturing the text after the first if statement. Is there a better way to do it? Could someone provide code?
My code is below. what i want is like in this iimage:
var currentbot = 0;
var awnswers = [];
app.post('/webhook', function(req, res) {
var events = req.body.entry[0].messaging;
for (i = 0; i < events.length; i++) {
var event = events[i];
if (event.message && event.message.text) {
var text = event.message.text;
if (text == "hi") {
start(event.message.text, event.sender.id);
}
}
}
res.sendStatus(200);
});
var awnswers = [];
function start(text, id) {
if (count == 0) {
sendTextMessage('hello lets order!', id);
arr.push(text);
console.log(awnswers);
count = 1;
} else if (count == 1) {
sendTextMessage('what size do you want?', id);
arr.push(text);
console.log(awnswers);
count = 2;
} else if (count == 2) {
sendTextMessage('its on its way!', id);
arr.push(text);
console.log(awnswers);
count = 0;
}
}
function sendTextMessage(messageText, recipientId) {
var messageData = {
recipient: {
id: recipientId
},
message: {
text: messageText
}
};
callSendAPI(messageData);
}
function callSendAPI(messageData) {
request({
uri: 'https://graph.facebook.com/v2.6/me/messages',
qs: {
access_token: process.env.access_token
},
method: 'POST',
json: messageData
}, function(error, response, body) {
if (!error && response.statusCode == 200) {
var recipientId = body.recipient_id;
var messageId = body.message_id;
console.log("Successfully sent generic message with id %s to recipient %s", messageId, recipientId);
} else {
console.error("Unable to send message.");
console.error(response);
console.error(error);
}
});
}
The main issues I think I see are:
Start() is only called when text == hi
Count is not defined
You're pushing to the array 'arr' not, awnswers
You can fix these by:
Calling start() on every message
Defining count like var count = 0; at the top of your file, next to var currentbot
awnswers.push(text);

How to check if text is found in column in Protractor

I'm trying to assert that a name is displayed in a column of a table. I've written an inResults function that will iterate through a column's text to see if a name exists. Here's what I'm trying:
Page object:
this.names = element.all(by.repeater('row in rows').column('{{row}}'));
this.inResults = function(nameString) {
var foundit = '';
this.names.each(function(name) {
name.getText().then(function(it) {
console.log(it); // each name IS printed...
if(it == nameString) {
console.log('it\'s TRUE!!!!'); // this gets printed...
foundit = true;
}
});
});
return foundit; // returns '' but should be true?
};
Spec expect:
expect(friendPage.inResults('Jo')).toBeTruthy();
Both console statements print as expected... but my expect fails as foundit's value is still ''. I've tried this a number of ways and none are working. What am I missing?
I've devised what I think is a better/cleaner way to solve this. It's less complex and doesn't require locator/css code in the method.
friend.page.js
// locator
this.friendName = function(text) { return element.all(by.cssContainingText('td.ng-binding', text)) };
// method
this.inResults = function(name) {
return this.friendName(name).then(function(found) {
return found.length > 0;
});
};
friend.spec.js
expect(friendPage.inResults('Jo')).toBeTruthy();
I've added this to my protractor_example project on GitHub...
I would recommend you to use filter: http://angular.github.io/protractor/#/api?view=ElementArrayFinder.prototype.filter
this.inResults = function(nameString) {
return this.names.filter(function(name) {
return name.getText().then(function(text) {
return text === nameString;
});
}).then(function(filteredElements) {
// Only the elements that passed the filter will be here. This is an array.
return filteredElements.length > 0;
});
});
// This will be a promise that resolves to a boolean.
expect(friendPage.inResults('Jo')).toBe(true);
Use map to do this.This will return a deferred that will resolve with the values in an array, so if you have this:
this.mappedVals =element.all(by.repeater('row in rows').column('{{row}}')).map(function (elm) {
return elm.getText();
});
It will resolve like this:
this.inResults = function(nameString) {
var foundit = '';
mappedVals.then(function (textArr) {
// textArr will be an actual JS array of the text from each node in your repeater
for(var i=0; i<textArr.length; i++){
if(it == textArr[i]) {
console.log('it\'s TRUE!!!!'); // this gets printed...
foundit = true;
}
}
return foundit;
});
}
And Use that in Spec file like,
friendPage.inResults('Jo').then(function(findIt){
expect(findIt).toBeTruthy();
});

How to get the PFRelation objects while getting the PFquery objects?

NSPredicate *predicate=[NSPredicate predicateWithFormat:#"(UserId==%#)",[defaluts objectForKey:#"objectId"]];
PFQuery *frndquery=[PFQuery queryWithClassName:#"FriendsDetails" predicate:predicate];
[frndquery orderByDescending:#"lastdate"];
[frndquery whereKey:#"BlockStatus" equalTo:#"No"];
NSArray *arrquery=[frndquery findObjects];
for (PFObject *frndids in arr){
PFRelation *relation=[frndids relationforKey:#"ChatRelation"];
NSArray *arrids=[NSArray arrayWithObjects:[frndids objectForKey:#"UserId"],[frndids objectForKey:#"ConversationID"], nil];
PFQuery *statusQuery = [relation query];
[statusQuery orderByDescending:#"createdAt"];
[statusQuery whereKey:#"Deletechat" notContainedIn:arrids];
statusQuery.limit = [[NSNumber numberWithInt:1] intValue];
NSArray *arrforrelationobjects=[statusQuery findObjects];}
I want to find all objects when we retrieve the objects from the first query itself. Please solve my problem
There is a method you can use to include properties which are pointer values. You cannot use the include method with relations. What I do instead is use a Cloud Code function to aggregate the results I want into a JSON object and return that object.
See the fetchPostDetails function in the following script.
https://github.com/brennanMKE/PostThings/blob/master/Parse/PostThings/cloud/main.js
It fetches items are relation objects such as tags and likes which happen to be User objects which are relations to the Post class. There are also comments which are referenced as a pointer back to the post from each comment. The fetchPostTags and fetchPostLikes methods show how to fetch those relations and populate the JSON object which is holding all of the results. You need to deploy those Cloud Code update and then access it as a function from the iOS side. The results will come back as an NSDictionary with values for posts, tags, likes and comments. The posts are an array of Post objects. The tags, likes and comments are NSDictionary objects which have the postId as the key to access the array of Parse objects.
This way one call to the function will get you want you need.
I've included some of the code below as a reference in case what is on GitHub changes.
// Helper functions in PT namespace
var PT = {
eachItem : function (items, callback) {
var index = 0;
var promise = new Parse.Promise();
var continueWhile = function(nextItemFunction, asyncFunction) {
var item = nextItemFunction();
if (item) {
asyncFunction(item).then(function() {
continueWhile(nextItemFunction, asyncFunction);
});
}
else {
promise.resolve();
}
};
var nextItem = function() {
if (index < items.length) {
var item = items[index];
index++;
return item;
}
else {
return null;
}
};
continueWhile(nextItem, callback);
return promise;
},
arrayContainsItem : function(array, item) {
// True if item is in array
var i = array.length;
while (i--) {
if (array[i] === item) {
return true;
}
}
return false;
},
arrayContainsOtherArray : function(array, otherArray) {
/// True if each item in other array is in array
var i = otherArray.length;
while (i--) {
if (!PT.arrayContainsItem(array, otherArray[i])) {
return false;
}
}
return true;
},
fetchPostTags : function(post) {
return post.relation("tags").query().find();
},
fetchPostLikes : function(post) {
return post.relation("likes").query().find();
},
fetchPostComments : function(post) {
var query = new Parse.Query(Comment);
query.include("owner");
query.equalTo("post", post);
return query.find();
},
fetchPostDetails : function(post, json) {
json.tags[post.id] = [];
json.likes[post.id] = [];
json.comments[post.id] = [];
return PT.fetchPostTags(post).then(function(tags) {
json.tags[post.id] = tags;
return PT.fetchPostLikes(post);
}).then(function(likes) {
json.likes[post.id] = likes;
return PT.fetchPostComments(post);
}).then(function(comments) {
json.comments[post.id] = comments;
json.count++;
return Parse.Promise.as();
});
},
};