Google幻灯片:找不到新插入的表格 [英] Google Slides: newly inserted table not found

查看:115
本文介绍了Google幻灯片:找不到新插入的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道发生了什么事.我有两个函数,当一个接一个地调用时,两个函数都运行良好:

I´m wondering what is going on. I have two functions which both are working good when called one after one:

function createTable() {
  var slidesPage = SlidesApp.openById('1QWRV4eQzGNNBz4SkR3WPurTL3O60oGYxQpBu63KrUoI').getSlides()[0];

  var table = slidesPage.insertTable(7, 4);
}


function changeColumnWidth() {

  var slidesPage = SlidesApp.openById('1QWRV4eQzGNNBz4SkR3WPurTL3O60oGYxQpBu63KrUoI').getSlides()[0];

  var tableId = slidesPage.getTables()[0].getObjectId();

  var requests = [{
    updateTableColumnProperties: {
      objectId: tableId,
      "columnIndices": [ 1, 3],
      "tableColumnProperties": {
        "columnWidth": {
          "magnitude": 80,
          "unit": "PT"
         }
       },
      "fields": "columnWidth"
     }
   }];

  var createSlideResponse = Slides.Presentations.batchUpdate({
    requests: requests
  }, '1QWRV4eQzGNNBz4SkR3WPurTL3O60oGYxQpBu63KrUoI');

}

但是尝试结合这两个功能,例如:

But trying to combine these two functions like:

function combined() {
   createTable();
   changeColumnWidth();
}

我遇到错误:

无效请求[0].updateTableColumnProperties:找不到对象(SLIDES_API456304911_0).

Invalid requests[0].updateTableColumnProperties: The object (SLIDES_API456304911_0) could not be found.

想知道insertTable方法是否异步,因此创建的表尚未准备好吗? 谢谢你的帮助.

Wondering if the insertTable method is asynchronous and therefore the created table is not ready? Thanks for any help.

推荐答案

要实现您的目标-在一个函数中创建具有指定布局和特定列大小的表-您应该对整个任务使用Slides API.如果您提供了唯一的对象ID,则可以使用Slides API在同一个批处理请求中创建和修改相同的元素.否则,您必须先创建该元素,然后使用对第一个请求的响应中找到的objectId发送修改请求.第二种方法实质上是您分别完成函数调用时所遇到的行为.

To achieve your goal - create a table with a specified layout and specific column sizes in one function - you should use the Slides API for the entire task. The Slides API lets you both create and modify the same element in the same batch request, if you provided a unique object ID for it. Otherwise, you have to first create the element, then send the modification request using the objectId found in the response to the first request. This second approach is essentially the behavior you were experiencing when the function calls were done separately.

自然,用户提供的ID受到限制:

There are restrictions on user-supplied IDs, naturally:

objectId string :用户提供的对象ID.

如果指定ID,则该ID在所有页面和页面元素中必须唯一演示文稿.该ID必须以字母数字字符或下划线开头(与regex [a-zA-Z0-9 _] 匹配);其余字符可能包括连字符和冒号(与正则表达式 [a-zA-Z0-9 _-:] 匹配). ID的长度不能小于5或大于50.

如果不指定ID,则会生成一个唯一的ID.

objectId string: A user-supplied object ID.

If you specify an ID, it must be unique among all pages and page elements in the presentation. The ID must start with an alphanumeric character or an underscore (matches regex [a-zA-Z0-9_] ); remaining characters may include those as well as a hyphen or colon (matches regex [a-zA-Z0-9_-:] ). The length of the ID must not be less than 5 or greater than 50.

If you don't specify an ID, a unique one is generated.

鉴于允许使用连字符,我们可以使用Utilites.getUuid()方法来帮助提供我们自己的唯一对象ID.

Given that hyphens are allowed, we can use the Utilites.getUuid() method to help supply our own unique object IDs.

在混合SlidesAppSlides时,很可能内部Google优化(例如,写缓存)会更改操作顺序.通过限制相关任务操作的单一服务,我们可以确保所需的对象在需要时可用.

When mixing SlidesApp and Slides, it is very likely that internal Google optimizations (e.g. write-caching) change the operation order. By restricting to a single service for related task operations, we can ensure that the objects we need are available when needed.

此示例使用两种方法制作 Request batchUpdate的对象,并最终创建演示文稿,添加空白幻灯片,添加表格并对其进行修改,然后创建另一个空白幻灯片.

This example uses two methods that make Request objects for batchUpdate and ultimately creates a presentation, adds a blank slide, adds a table and modifies it, and then creates another blank slide.

function makeCreateTableRequest_(slideId, rows, columns, shouldSupplyID) {
  const tablerq = {
    rows: rows,
    columns: columns,
    elementProperties: {
      pageObjectId: slideId,
  /** size: {
        height: {...},
        width: {...}
      },
      transform: { ... } */
    }
  };

  // If asked to use a custom ID (e.g. also going to modify this table), use a unique one.
  if (shouldSupplyID)
    tablerq.objectId = ("table" + Utilities.getUuid()).slice(0, 50);

  return {createTable: tablerq};
}
function makeModifyTableColumnPropsRequest_(tableId, newWidthDimension, indicesArray) {
  const rq = {
    objectId: tableId,
    fields: "columnWidth" // There are no other fields for this request as of 2018-07
  };
  if (newWidthDimension && newWidthDimension.magnitude !== undefined && newWidthDimension.unit)
    rq.tableColumnProperties = { columnWidth: newWidthDimension };
  if (indicesArray && indicesArray.length)
    rq.columnIndices = indicesArray;
  return {updateTableColumnProperties: rq};
}
function createPresentation_() {
  const newPres = { title: "API-created Presentation" };
  // Presentations are huge... limit the metadata sent back to us.
  const fields = "presentationId,pageSize,title"
    + ",slides(objectId,pageType,pageElements(objectId,size,title,description))"
    + ",masters(objectId,pageType,pageElements(objectId,size,title,description))"
    + ",layouts(objectId,pageType,pageElements(objectId,size,title,description))";
  const createdMetadata = Slides.Presentations.create(newPres, {fields: fields});
  console.log({message:"Created a Presentation", response: createdMetadata});
  return createdMetadata;
}
function addSlide_(pId) {
  const response = Slides.Presentations.batchUpdate({ requests: [{ createSlide: {} }] }, pId);
  return response.replies[0].createSlide.objectId;
}

function foo() {
  const pres = createPresentation_();
  const newSlideId = addSlide_(pres.presentationId);

  // Get requests to add and to modify tables.
  const openingTableRq = makeCreateTableRequest_(pres.slides[0].objectId, 2, 4);
  const newTableRq = makeCreateTableRequest_(newSlideId, 7, 4, true);
  const changeWidthRq = makeModifyTableColumnPropsRequest_(newTableRq.createTable.objectId, {magnitude: 80, unit: "PT"}, [0]);
  // Add and update the desired table, then create a new slide.
  var response = Slides.Presentations.batchUpdate({
      requests: [
        openingTableRq, // will have reply
        newTableRq, // will have reply
        changeWidthRq, // no reply
        { createSlide: {} } // will have reply
      ]
    }, pres.presentationId);
  console.log({message: "Performed updates to the created presentation", response: response});
}

这篇关于Google幻灯片:找不到新插入的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆