1

摘要

window.Open('')或者window.Open('about:blank')在一个普通的 html 文件中使用 JavaScript,可以在这里测试。但它似乎不适用于 Office.js 加载项。

详情

在我Windows 10 desktop的帮助下,VS2017我创建了这个Office.js WORD 插件项目,它与我的Microsoft Office Home and Student 2016版本完美配合。在同一个项目中,然后我btnTesthome.js文件中创建了一个新按钮。当我单击btnTest它成功调用以下MyTest方法并打开一个带有window.Open('some URL').

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

这里的目标是我的代码根据其中的一些内容创建 HTML 字符串WORD document,然后使用window.document.write(...)方法在浏览器中动态打开该 html,如解释(并且您可以测试)here问题:我怎样才能使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));
                }
            });
    }

window.open('about:blank');调用时弹出窗口以下消息框,确定按钮变灰:

在此处输入图像描述

注意:在同一个桌面上,然后我创建了新的这个 UWP AP with Javascript项目,并在他们项目的以下方法中,我取消了他们的代码并添加了window.open('about:blank');. 当我在那里调用以下方法时,它会在那里成功打开默认的空白页面。

function sayHello() {
    //var messageDialog = new Windows.UI.Popups.MessageDialog("Hello, world!", "Alert");
    //messageDialog.showAsync();
    window.open('about:blank');
}

更新

我怎样才能让它从对话框中工作?我尝试了以下方法,但没有成功:我创建了这个对话框 Add-In。它按原样工作。然后我在新项目中将插件文件中的submit()函数代码注释掉并添加了行。当我单击对话框的提交按钮时,它会在默认浏览器中成功打开谷歌网站。但是如果我将上面的行替换为Form.jswindow.open('https://www.google.com/');

var myWindow = window.open('about:blank');
myWindow.document.write('html here'');`

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

4

2 回答 2

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}`);
    }
  }
}
于 2019-02-25T17:24:45.973 回答
0

我们在沙盒级别阻止这些调用,我强烈建议您使用此处建议的 ShowDialog API是否有任何 Office.js API 来获取用户输入

于 2018-11-08T20:55:19.113 回答