add Office.onReady() function to javascript - javascript

I am new to Javascript and am trying to make an outlook web-addin take the Office.onReady() function properly.
https://learn.microsoft.com/en-us/office/dev/add-ins/develop/initialize-add-in
I tried using
Office.onReady()
.then
to make my function work with the office js API but it then I got an error that my function couldn't be found.
Code:
$("#brief-summary").click(briefsummary);
$("#email-setup").click(emailsetup);
Office.onReady()
.then(function briefsummary() {
var msgFrom = Office.context.mailbox.item.from;
var msgfirstname = String(msgFrom.displayName).split(" ");
Office.context.mailbox.item.displayReplyAllFormAsync(
"Hello " +
msgfirstname[0] +
", <br> \
<br> Here is a brief summary on everything worked on: \
<br> \
<br>Thank you for your time, \
<br>"
);
});
Result:
Uncaught ReferenceError: briefsummary is not defined
So I am pretty sure I am doing it wrong.
I also tried using Office.onReady(); at the begining of the script. It suppressed the Uncaught Error: Office.js has not fully loaded. Your app must call "Office.onReady()" error but office javascript doesn't do anything. (Its suppose to bring up a new reply window with text inside)
Link to where the app is hosted: https://alloyautomateaddinbeta3.azurewebsites.net/index.html (Although the office.js only loads when you are in the outlook app. But you can go to sources from inspector in chrome and look at the js script in full)
Any ideas would be awesome.

I have seems to figure out what to do but I was pretty janky. If anyone knowas a better way to clean this up let me know.
I essentially my code inbetween this and it worked:
Office.onReady((info) => {
if (info.host === Office.HostType.Outlook) {
#Insert code here
}
});
It was pretty janky as it gives me a warning saying Function variables should not be placed in blocks. Use a function expression or move the statement to the top of the outer function
I will be working on the code periodically so I will update my answer to a better one as time goes on unless someone else has an answer as well.
Thanks

Related

Add HyperLink With Word JavaScript API

I'm having difficulties to add an HyperLink to my Word Document using the Javascript API. I've look to Doc and I can't find any hints how to accomplish my duty...
Here is my Question: What is the best way to add an HyperLink inside a Word Document using the Javascript API.
And Here is what I tried:
Word.run((context: Word.RequestContext) => {
var range = context.document.getSelection();
context.load(range, "hyperlink");
return context.sync().then(() => {
range.font.highlightColor = '#FFFF00';
range.hyperlink = "C:\My Documents\MyFile.doc";
}).then(context.sync);
});
I've added the highlightColor just to have a visual that my changes are being sync. Everything seems fine but the Hyperlink property is not being updated. Am I missing something?
And If you guys are wondering what's this syntax, I'm using TypeScript.
Good, if you don't mind i will reply in JavaScript :)
Setting a hyperlink to a file must work (provided that the file exists :) ). I have this simplified example working successfully, btw you don't need to load the range for setting this.
Also hyperlinks is now supported as preview, so please make sure that you are running an updated (latest) version of Word (go file and install updates) and most importantly make sure you are using the preview CDN for Office.js which is here: https://appsforoffice.microsoft.com/lib/beta/hosted/office.js
Word.run(function(context) {
// Insert your code here. For example:
context.document.getSelection().hyperlink = "C:\My Documents\MyFile.doc";
return context.sync();
});

console output does not appear in code wars

I'm doing some javascript exercises on code wars. I want to see what's going wrong in my programs by printing to the console, but nothing except the test results appears in the output window. Does anyone know how to print to the console in code wars? I can't find anything in their documentation.
function areYouPlayingBanjo(name) {
// Implement me
var person = name.split('');
person[0].toLowerCase();
console.log(person[0]);
if(person[0] === 'r'){
return name + " plays banjo";
}
else{
return name + " does not play banjo";
}
}
This is not exactly an answer to the question, but more of an extended answer in order to document a chrome extension that can allow debugging in the console for codewars.
Install this chrome extension:
https://github.com/bojan88/Codewars-JavaScript-debugger
This allows you to use the debugger; statement to force the your code to run in the browser rather than the sandboxed environment on the codewars servers. Works great.
Caveat: I don't know if this is safe. Use at your own risk.
This worked for me:
Console.Out.WriteLine("Print stuff to output here.");

Cannot find source of javascript function call

Ok, so I need to find the source code of a particular javascript function on a website. (The specifics do not really matter unless there is no way to do what it is that I am asking)
I can see the function call in a link of html code
onclick="inbox.sendMessage();"
I know that the function does work because if I use a plugin a can call the function on that page, however, I have searched every .js file is referenced in that page, and none of them contain a function called sendMessage.
What I am asking is, is there a way to follow the code back to the source, perhaps if there was a way to debug the html and break when the onclick is triggered and then step into the function to see its source, but I do not know how I can do that or if it is even possible. Any help will be greatly appreciated, Thanks.
I guess you could do :
inbox.sendMessage
In the webconsole. (the function name without the parenthesis)
It will print out the source code of the function.
I usually use Opera, and in that at least this is what I do:
Open Opera Dragonfly (Ctrl + Shift + I).
Click on the HTML tag with the onclick handler.
Go to the listeners tab in the right hand side column.
See the listener for the click event. It shows you the file and line number.
sendMessage could be declared as:
var inbox{
sendMesssage:function(){
}
}
//or
function inbox(){
this.sendMessage=function(){
}
}
// or
inbox.sendMessage=function(){}
// or ...
So looking for "sendMessage(" or "function sendMessage" will not find you anything.
In chrome, Internet Explorer and Firefox (with firebug) you can hit F12 and go to debug, there you can check the scripts that have been loaded as there might have been scripts loaded dynamically.
#!/usr/bin/env ruby
Dir::glob("*").each do |name|
lineCount = 1
File.open(name, "r").each do |line|
puts "\nFile name: " + name + "\nline: " + lineCount.to_s if line =~ /inbox.sendMessage/ && name != "findfunction.rb"
lineCount += 1
end
end
Wrote a quick ruby script to help you out. To execute, first make sure you have a ruby interpreter on your machine then place the script in the directory with all your relevant files. load up a command line terminal, navigate to said directory and type "ruby findfunction.rb".
It will tell you all instances (files + line number) of "inbox.sendMessage".

ReferenceError: "document" is not defined

Im new to JavaScript and even more new to Google Apps Script. Im trying a simple function that shows the current date (only day, month e and full year), but the Google Script show the error ReferenceError: "document" is not defined.
My goal is to use this function in a Google Site. Here is the code:
function Data()
{
var d=new Date();
var dia=d.getDate();
var mes=d.getMonth();
var ano=d.getFullYear();
var DataCompleta=dia + "/" + mes + "/" + ano
document.write(DataCompleta);
}
Code running as a Google Apps Script does not run in the browser, so you cannot use web browser APIs with it. If you want to output content to a Google Site, then you need to use the API for Sites.
Presumably you would want something like createWebPage and then use the methods on the resulting object to add the content to it.
As said in the former answer you can't execute a function directly in the Browser, you'll have to choose a so called 'container' to run your function from it. I would recommand you read the documentation and maybe try a few simple tutorials to see how GAS can be executed.
EDIT : following your comments, feel free to have a look at this script built with UiApp, the result is viewable here and shows what you wanted to : "Hello, today is 25/10/2012"

Firebug not showing Javascript errors for Rails applications

I have a Rails application, and when I have Javascript errors they are not showing in the Firebug console. I have 'Show javascript errors' and 'Show javascript warnings' selected.
When I insert javascript errors in a basic html file, the errors show as expected.
In the javascript of the Rails app, it only shows errors in rare cases.
For example i can insert nonsense like:
dfghaefb;
and no error is shown in Firebug. But if i insert a space in there Firebug does show the error:
dfgh aefb;
Any ideas? This is driving me nuts.
UPDATE:
Pumbaa80 was right, it's syntax vs runtime.
So I set up onerror:
onerror=errorHandler;
var error="";
function errorHandler(errMessage,url,line){
error="There is an error at this page.\n";
error+="Error: " + errMessage+ "\n";
error+="URL: " + url + "\n";
error+="Line: " + line + "\n\n";
error+="Click OK to continue viewing this page,\n";
alert(error);
return true;
}
And I have a method with an error:
function initForm() {
asdfs;
}
And it works when I call it outside of a method:
initForm();
but not in this case:
document.observe('dom:loaded', function() {
initForm();
});
Why is that?
dfghaefb;
produces a run-time error. Those errors may be suppressed by putting an onerror handler on the window or by trapping them in a try/catch in some way. In that case, Firebug won't show anything.
In contrast,
dfgh aefb;
is a syntax error, which is shown in the error console, regardless of try/catch and onerror.
Did you let the page load completely? I have a rails app running in front of me right now with firebug on and i entered dfghaefb; and yes it does throw an error!
ReferenceError: dfghaefb is not defined { message="dfghaefb is not defined", more...}
Running Firefox 3.6.3, Firebug 1.5.3 and MacOSX 10.5.6 :)
This may appear to be obvious, but be sure you wrap your inline JS using
<script type="text/javascript"> code </script>
Otherwise FireBug won't look at it at all.
I had the same, it wasn't showing anything and it was driving me crazy for ages, even if i had "Show javascript errors" in the console tab selected, it seems that it started working after I did "Enable all panels".
These kinds of problems cannot be answered without a test case.

Categories

Resources