Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/windows-phone-7/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Typescript 设置InlinePicture宽度时的常规异常_Typescript_Ms Word_Office Js - Fatal编程技术网

Typescript 设置InlinePicture宽度时的常规异常

Typescript 设置InlinePicture宽度时的常规异常,typescript,ms-word,office-js,Typescript,Ms Word,Office Js,上下文 目前,我正在开发一个Word插件,它使用新的Office Javascript API。外接程序由一些基本功能和一些模板组成。模板与自动打开的任务窗格相结合,用户可以在其中配置模板并填写所需信息。在此任务窗格中,用户可以更改标题中的图像。要求用户从一组预定义图像中选择一个图像,然后将这些图像放置在内容控件中 问题 图像的位置正确,但是,每当我尝试设置图像的宽度时,我都会收到一个GeneralException。奇怪的是,它在某些情况下确实有效(通常是我调整它的前1到3次)。根据我以前的经

上下文

目前,我正在开发一个Word插件,它使用新的Office Javascript API。外接程序由一些基本功能和一些模板组成。模板与自动打开的任务窗格相结合,用户可以在其中配置模板并填写所需信息。在此任务窗格中,用户可以更改标题中的图像。要求用户从一组预定义图像中选择一个图像,然后将这些图像放置在内容控件中

问题

图像的位置正确,但是,每当我尝试设置图像的宽度时,我都会收到一个
GeneralException
。奇怪的是,它在某些情况下确实有效(通常是我调整它的前1到3次)。根据我以前的经验,我知道如果内容控件被锁定,可能会导致
一般异常。然而,这里的情况似乎并非如此。为了使它工作,我尝试了以下方法

  • 添加段落并在其中添加图片,而不是直接将其添加到内容控件
  • 完全删除了编辑保护
  • 试过不同的宽度
  • 在上下文同步后调整图片宽度
代码

”picture.width=71导致有问题的错误

// Needed to interact with Word in typescript.
declare const Word: any;

export interface IImage {
  imageId: string;
  base64String: string;
}

export function InsertImageInControl(imageId: string, controlTitle: string) {
  Word.run(function(context) {
    // Retrieves content control.
    var contentControl = context.document.contentControls
      .getByTitle(controlTitle)
      .getFirst();

    // Disable editing protection.
    contentControl.cannotEdit = false;

    return context
      .sync()
      .then(function() {
        FetchBase64Image(imageId)
          .then(function(image) {
            let picture = contentControl.insertInlinePictureFromBase64(
              image.base64String,
              "Replace"
            );

            // Line that causes the error:
            picture.width = 71;

            // Reenable editing protection.
            contentControl.cannotEdit = true;
          })
          .then(context.sync)
          .catch(function(error) {
            console.log(error);
          });
      })
      .catch(function(error) {
        console.log(error);
      });
  });
}

export function FetchBase64Image(imageId: string): Promise<IImage> {
  return new Promise<IImage>(function(resolve, reject) {
    fetch("api/Images/" + imageId)
      .then(function(response) {
        resolve(response.json() as Promise<IImage>);
      })
      .catch(function(error) {
        reject(error);
      });
  });
}
//需要与typescript中的Word交互。
声明常量字:任何;
导出接口IImage{
imageId:字符串;
base64String:字符串;
}
导出功能InsertImageInControl(图像ID:字符串,控件标题:字符串){
运行(函数(上下文){
//检索内容控件。
var contentControl=context.document.contentControls
.getByTitle(controlTitle)
.getFirst();
//禁用编辑保护。
contentControl.cannotEdit=false;
返回上下文
.sync()
.然后(函数(){
FetchBase64图像(imageId)
.然后(函数(图像){
让picture=contentControl.insertInlinePictureFromBase64(
image.base64String,
“替换”
);
//导致错误的行:
图片宽度=71;
//可重入编辑保护。
contentControl.cannotEdit=true;
})
.then(context.sync)
.catch(函数(错误){
console.log(错误);
});
})
.catch(函数(错误){
console.log(错误);
});
});
}
导出函数FetchBase64Image(imageId:string):Promise{
返回新承诺(功能(解决、拒绝){
获取(“api/Images/”+imageId)
.然后(功能(响应){
解析(response.json()作为承诺);
})
.catch(函数(错误){
拒绝(错误);
});
});
}

谢谢你的提问。我在玩这个和一些评论

  • 不确定您是否意识到这一点,但我们也为Office.js提供了一个TypeScript库,无需创建自己的库
  • 我创建了一个基本上复制您的代码的示例。您可以使用来尝试我的代码片段。请在那里运行代码重要信息:确保在标题中添加带有“图像”标题的内容控件,以复制此内容。然后单击“插入图片”按钮,然后多次单击“更改宽度”按钮 有几次我都能得到一个异常,但只有当我真的很快地尝试设置图像宽度时。我认为这是一个竞赛条件,你试图改变内容控件的宽度,而内容控件是只读的。这应该是一段非常短的时间(毫秒?),您可以重试该操作,它应该可以工作

    请试一试

    顺便说一句,使用我们的TS库,您可以大大简化代码,并可以如下所示:

    异步函数changeWidth(){ 等待Word.run(异步(上下文)=>{ //解锁cc context.document.contentControls.getByTitle(“图像”).getFirst().cannotEdit=false; wait context.sync(); //在cc宽度内设置图像。。。 context.document.contentControls.getByTitle(“图像”).getFirst().inlinePictures.getFirst().width=Math.floor((Math.random()*200)+50);; wait context.sync(); //锁cc context.document.contentControls.getByTitle(“图像”).getFirst().cannotEdit=true; wait context.sync(); console.log(“成功!”); });
    }
  • 谢谢@Juan!不幸的是,即使重试,我也会不断收到错误。我想我现在会用另一种方法来解决这个问题。你是在什么版本中观察到这个问题的?版本1803,版本9126.2152