Office.js Web WORD加载项:window.open()方法不适用于“ about:blank” URL

时间:2018-10-28 01:30:24

标签: office-js office-js-helpers

摘要

window.Open('')window.Open('about:blank')与普通html文件中的JavaScript一起使用,可以进行测试here。但这似乎不适用于Office.js加载项。

详细信息

Windows 10 desktopVS2017上,我创建了this Office.js WORD加载项项目,该项目与我的Microsoft Office Home and Student 2016版本完美地兼容。然后,在同一项目中,我在btnTest文件中创建了一个新按钮home.js。当我单击btnTest时,它会成功调用以下MyTest方法并使用window.Open('some URL')打开一个新窗口。

但是,当我调用MyTest时,在相同的window.Open('about:blank')方法中,它不会打开blank page;而是打开Windows 10消息框,如下面的屏幕截图所示。

这里的目标是我的代码根据WORD document中的某些内容创建HTML字符串,然后使用window.document.write(...)方法在浏览器中动态打开该html,如所解释的(您可以测试){{ 3}}。 问题:如何使window.Open('about:blank')方法有效?

function MyTest() {
        Word.run(function (context) {

            // window.open('https://www.google.com/'); this works
            var myWindow = window.open('about:blank'); //this does not work and, instead, displays the default Windows 10 message shown in screenshot below
            myWindow.document.write('<!DOCTYPE html><html><head></head><body><p>This is just a paragraph</p></body></html>');


            // Synchronize the document state by executing the queued commands,
            // and return a promise to indicate task completion.
            return context.sync().then(function () {
                //following (after un-commenting) does not work either
                //var myWindow = window.open('about:blank');
                //myWindow.document.write('<!DOCTYPE html><html><head></head><body><p>This is just a paragraph</p></body></html>');
            });
        })
            .catch(function (error) {
                console.log('Error: ' + JSON.stringify(error));
                if (error instanceof OfficeExtension.Error) {
                    console.log('Debug info: ' + JSON.stringify(error.debugInfo));
                }
            });
    }

弹出Windows后续消息框,并在调用 window.open('about:blank');时将“确定”按钮显示为灰色:

here

注意:在同一个桌面上,我创建了一个新的enter image description here UWP AP with Javascript项目,并在其项目的以下方法中,取消了对其代码的注释并添加了{ {1}}。当我在其中调用以下方法时,它将成功在那里打开默认空白页。

window.open('about:blank');

更新

如何通过对话框使其正常工作?我尝试了以下操作,但没有成功:我创建了此this。它按原样工作。然后,我在新项目的加载项的function sayHello() { //var messageDialog = new Windows.UI.Popups.MessageDialog("Hello, world!", "Alert"); //messageDialog.showAsync(); window.open('about:blank'); } 文件中注释了submit()函数的代码,并添加了Form.js行。当我单击对话框的“提交”按钮时,它将在默认浏览器中成功打开google网站。但是如果我将上面的行替换为

window.open('https://www.google.com/');

显示与上图相同的警告窗口

2 个答案:

答案 0 :(得分:0)

我们在沙盒级别阻止了这些调用,我强烈建议您按照此处的建议{@ 3}使用ShowDialog API

答案 1 :(得分:0)

import {POP_UP_HEIGHT, POP_UP_WIDTH} from '../constants';

/**
 * Handles interaction with the Office API for Common shared API.
 */
export default class Office {
  constructor(office) {
    this.office = office;
    this.dialog = null;
    this.callback = null;
  }

  /**
   * Store the callback function that will be invoked to
   * after pop-up message is received.
   * @param {string} url
   * @param {function} callback
   */
  openPopUp(url, callback) {
    this.callback = callback;
    this.office.context.ui.displayDialogAsync(url,
      {height: POP_UP_HEIGHT, width: POP_UP_WIDTH}, this.dialogCallback.bind(this)); // THIS IS WHAT YOU NEED
  }

  /**
  * Send the message from the child window (pop-up window)
  * To the parent window (Task pane window)
  * @param {string} message
  */
  messageFromPopUp(message) {
    this.office.context.ui.messageParent(message);
  }

  /**
   * The parent window will close the child window (pop-up)
   * and invoke the callback functionality
   * with a given message from the child window
   * @param {Object} event
   */
  dialogHandler(event) {
    this.dialog.close();
    this.callback(event.message);
  }

  /**
   * Store the child window (pop-up window) and create
   * an event handler to notify the parent window when
   * the child window sends a message to it
   * @param {Object} asyncResults
   * @param {string} asyncResults.status Status of the result, preferably 'succeeded'
   * @param {string} asyncResults.value
   */
  dialogCallback(asyncResults) {
    if (asyncResults.status === 'succeeded') {
      this.dialog = asyncResults.value;
      this.dialog.addEventHandler(this.office.EventType.DialogMessageReceived,
        this.dialogHandler.bind(this));
    }
    else {
      console.error(`Error: ${asyncResults.error.message}`);
    }
  }
}