Using functions in Google Apps Script to build a request and submit updates through batchUpdate with Google Docs API

huangapple go评论57阅读模式
英文:

Using functions in Google Apps Script to build a request and submit updates through batchUpdate with Google Docs API

问题

I have merged the cells of a given table in a Google Doc using Google Docs API through a function in Google Apps Script (i.e. javascript).

我的函数通过Google Apps脚本(即JavaScript)使用Google Docs API合并了给定表格的单元格。

My function goes through the following steps:

我的函数执行以下步骤:

  1. Loop through the content of a document.

  2. 循环遍历文档的内容。

  3. Identify a table according to an id located above it marca_tabla.

  4. 根据位于其上方的ID标识表格 marca_tabla

  5. Build the requests variable.

  6. 构建 requests 变量。

  7. Submit this variable through batchUpdate.

  8. 通过 batchUpdate 提交此变量。

These steps result in merging the cells between celda_ini and celda_fin.

这些步骤导致合并 celda_inicelda_fin 之间的单元格。

function fun_fusion_cel_tbl(id_documento, marca_tabla, celda_ini, celda_fin){
  let cuerpo_api = Docs.Documents.get(id_documento).body.content;

  for(var i = 0; i < cuerpo_api.length; i++){
    //console.log('read object %s... is it a table?: %s' , i, cuerpo_api[i].table != null);
    if (cuerpo_api[i].table != null) {
      //console.log('previous object is paragraph?:', cuerpo_api[i-1].paragraph != null);
      let marca = cuerpo_api[i-1].paragraph.elements[0].textRun.content;
      marca = marca.replace('\n', '');
      //console.log('object %s : table id in doc %s', i, marca);
      if (marca == marca_tabla){
        indice = cuerpo_api[i].startIndex;
        i      = cuerpo_api.length;             // exit
      }      
    }
  }

  let requests = [{ 
    mergeTableCells : { 
      tableRange      : { 
        tableCellLocation : { 
          tableStartLocation : { index: indice },
          rowIndex           : celda_ini[0], 
          columnIndex        : celda_ini[1] 
                            },
        columnSpan           : celda_fin[1] - celda_ini[1] + 1,
        rowSpan              : celda_fin[0] - celda_ini[0] + 1  
                        } } }];
  Logger.log(requests);
  Docs.Documents.batchUpdate({ requests }, id_documento);
}

When I call this function from another function it works perfectly:

当我从另一个函数调用此函数时,它运行完美:

function my_function1(){
	let id_doc = DocumentApp.getActiveDocument().getId();
	fun_fusion_cel_tbl(id_doc, '<<table_01>>', [1, 2], [2, 3]);
}

Now I'm trying to build a function replacing the last code line Docs.Documents.batchUpdate({ requests }, id_documento); with return(requests); and call it from a main function like this:

现在我尝试构建一个函数,将最后一行代码替换为 Docs.Documents.batchUpdate({ requests }, id_documento); 并调用它,像这样

function my_function2(){
	let id_doc   = DocumentApp.getActiveDocument().getId();
	let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
	Logger.log(my_req);
	Docs.Documents.batchUpdate({ my_req }, id_doc);
}

Log output for requests is identical to output for my_req but I get the following error when running my_function2: GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with error: Invalid JSON payload received. Unknown name "my_req": Cannot find field.
What should I do to pass my_req to batchUpdate?

requests 的日志输出与 my_req 的输出相同,但是当运行 my_function2 时,我收到以下错误:GoogleJsonResponseException:调用 docs.documents.batchUpdate 时出错,错误消息为:无效的 JSON 负载。未知名称 "my_req":找不到字段。 我应该怎么做才能将 my_req 传递给 batchUpdate

You should pass my_req directly to batchUpdate without wrapping it in an object. Here's the corrected code for my_function2:

您应该直接将 my_req 传递给 batchUpdate,不要将其包装在对象中。以下是已更正的 my_function2 代码:

function my_function2(){
	let id_doc   = DocumentApp.getActiveDocument().getId();
	let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
	Logger.log(my_req);
	Docs.Documents.batchUpdate(my_req, id_doc);
}

By making this change, you will pass the my_req array directly to batchUpdate, which should resolve the error you were encountering.

英文:

I have merged the cells of a given table in a Google Doc using Google Docs API through a function in Google Apps Script (i.e. javascript).
My function goes through the following steps:

  1. Loop through the content of a document.
  2. Identify a table according to an id located above it marca_tabla.
  3. Build the requests variable.
  4. Submit this variable through batchUpdate.

These steps result in merging the cells between celda_ini and celda_fin.

function fun_fusion_cel_tbl(id_documento, marca_tabla, celda_ini, celda_fin){
  let cuerpo_api = Docs.Documents.get(id_documento).body.content;

  for(var i = 0; i < cuerpo_api.length; i++){
    //console.log('read object %s... is it a table?: %s' , i, cuerpo_api[i].table != null);
    if (cuerpo_api[i].table != null) {
      //console.log('previous object is paragraph?:', cuerpo_api[i-1].paragraph != null);
      let marca = cuerpo_api[i-1].paragraph.elements[0].textRun.content;
      marca = marca.replace('\n', '');
      //console.log('object %s : table id in doc %s', i, marca);
      if (marca == marca_tabla){
        indice = cuerpo_api[i].startIndex;
        i      = cuerpo_api.length;             // exit
      }      
    }
  }

  let requests = [{ 
    mergeTableCells : { 
      tableRange      : { 
        tableCellLocation : { 
          tableStartLocation : { index: indice },
          rowIndex           : celda_ini[0], 
          columnIndex        : celda_ini[1] 
                            },
        columnSpan           : celda_fin[1] - celda_ini[1] + 1,
        rowSpan              : celda_fin[0] - celda_ini[0] + 1  
                        } } }];
  Logger.log(requests);
  Docs.Documents.batchUpdate({ requests }, id_documento);
}

When I call this function from another function it works perfectly:

function my_function1(){
	let id_doc = DocumentApp.getActiveDocument().getId();
	fun_fusion_cel_tbl(id_doc, '<<table_01>>', [1, 2], [2, 3]);
}

Now I'm trying to build a function replacing the last code line Docs.Documents.batchUpdate({ requests }, id_documento); with return(requests); and call it from a main function like this:

function my_function2(){
	let id_doc   = DocumentApp.getActiveDocument().getId();
	let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
	Logger.log(my_req);
	Docs.Documents.batchUpdate({ my_req }, id_doc);
}

Log output for requests is identical to output for my_req but I get the following error when running my_function2: GoogleJsonResponseException: API call to docs.documents.batchUpdate failed with error: Invalid JSON payload received. Unknown name "my_req": Cannot find field.
What should I do to pass my_req to batchUpdate?

答案1

得分: 0

关于您的以下脚本。

function my_function2(){
    let id_doc   = DocumentApp.getActiveDocument().getId();
    let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
    Logger.log(my_req);
    Docs.Documents.batchUpdate({ my_req }, id_doc);
}

从"Log output for requests is identical to output for my_req"和"API call to docs.documents.batchUpdate failed with error: Invalid JSON payload received. Unknown name "my_req": Cannot find field.",如果您的"my_req"请求体是batchUpdate方法的有效请求体,可以考虑以下修改。

从:

Docs.Documents.batchUpdate({ my_req }, id_doc);

到:

Docs.Documents.batchUpdate({ requests: my_req }, id_doc);

或者,如果"my_req"是{requests: [,,,]},请按以下方式修改。

Docs.Documents.batchUpdate(my_req, id_doc);

或者,如果"my_req"是[,,,],可以使用以下修改。

从:

let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
Logger.log(my_req);
Docs.Documents.batchUpdate({ my_req }, id_doc);

到:

let requests = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
Logger.log(requests);
Docs.Documents.batchUpdate({ requests }, id_doc);

参考:

英文:

About your following script.

function my_function2(){
    let id_doc   = DocumentApp.getActiveDocument().getId();
    let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
    Logger.log(my_req);
    Docs.Documents.batchUpdate({ my_req }, id_doc);
}

From Log output for requests is identical to output for my_req and API call to docs.documents.batchUpdate failed with error: Invalid JSON payload received. Unknown name "my_req": Cannot find field., if your request body of my_req is valid request body for the batchUpdate method, how about the following modification?

From:

Docs.Documents.batchUpdate({ my_req }, id_doc);

To:

Docs.Documents.batchUpdate({ requests: my_req }, id_doc);

or, if my_req is {requests: [,,,]}, please modify as follows.

Docs.Documents.batchUpdate(my_req, id_doc);

or, if my_req is [,,,], the following modification can be used.

From:

let my_req = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
Logger.log(my_req);
Docs.Documents.batchUpdate({ my_req }, id_doc);

To:

let requests = fun_fusion_cel_tbl2(id_doc, '<<table_01>>', [1,2], [2,3]);
Logger.log(requests);
Docs.Documents.batchUpdate({ requests }, id_doc);

Reference:

huangapple
  • 本文由 发表于 2023年6月9日 02:32:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定