Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.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
Javascript 如何使用GoogleApps脚本复制和编辑Google文档中表格单元格内的文本数据_Javascript_Google Apps Script_Google Docs - Fatal编程技术网

Javascript 如何使用GoogleApps脚本复制和编辑Google文档中表格单元格内的文本数据

Javascript 如何使用GoogleApps脚本复制和编辑Google文档中表格单元格内的文本数据,javascript,google-apps-script,google-docs,Javascript,Google Apps Script,Google Docs,我是谷歌应用程序脚本的新手,已经陷入这个问题好几天了。提前感谢那些正在看这篇文章试图帮助我的人 我试图从某个表格单元格复制文本数据,用换行符将它们分割,然后将它们放入变量中,然后在另一个表格中使用它们。 使用tablecell.getText()会丢失所有格式,因此我想改用段落,但使用tablecell时无法使用GetPages() 我不知道我离目标有多近。 有没有一种方法可以在不丢失格式的情况下编辑文本数据 我相信你的目标如下 您希望使用Google Apps脚本实现表格单元格的转换,如问题

我是谷歌应用程序脚本的新手,已经陷入这个问题好几天了。提前感谢那些正在看这篇文章试图帮助我的人

我试图从某个表格单元格复制文本数据,用换行符将它们分割,然后将它们放入变量中,然后在另一个表格中使用它们。 使用tablecell.getText()会丢失所有格式,因此我想改用段落,但使用tablecell时无法使用GetPages()

我不知道我离目标有多近。 有没有一种方法可以在不丢失格式的情况下编辑文本数据


我相信你的目标如下

  • 您希望使用Google Apps脚本实现表格单元格的转换,如问题中所示。(下图来自您的问题。)

对于这个问题,这个答案如何?为了解决您的问题,我想提出以下示例脚本。此脚本的流程如下所示

  • 检索表
  • 检索表的单元格“B2”。
    • 这是来自您的示例图像
  • 创建包含文字和文字样式的对象。
    • 这用于将值拆分为单元格
  • 文本和文本样式设置为单元格
  • 当行数小于拆分值的行数时,将追加新行,并将文本和文本样式设置为单元格
  • 示例脚本: 请将以下脚本复制并粘贴到Google文档的容器绑定脚本中,并在脚本编辑器中运行
    myFunction
    功能。在这个脚本中,
    分别是行号和列号。因此,在您的图像中,请设置
    row=2
    column=2
    。例如,当您要拆分单元格“C3”时,请设置
    row=3
    column=3

    function myFunction() {
      // 1. Retrieve table.
      const body = DocumentApp.getActiveDocument().getBody();
      const table = body.getTables()[0];
      
      // 2. Retrieve the cell "B2" of the table.
      const row = 2;  // Please set the row number.
      const column = 2;  // Please set the column number.
      const cell = table.getCell(row - 1, column - 1);
      
      // 3. Create an object including the text and text styles. This is used for splitting values to the cells.
      const text = cell.editAsText();
      let temp = [];
      const textRuns = [].reduce.call(text.getText(), (ar, c, i, a) => {
        if (c != "\n") temp.push({text: c, foregroundColor: text.getForegroundColor(i), backgroundColor: text.getBackgroundColor(i), textAlignment: text.getTextAlignment(i), italic: text.isItalic(i)});
        if (c == "\n" || i == a.length - 1) {
          ar.push({text: temp.reduce((s, {text}) => s += text, ""), styles: temp});
          temp = [];
        }
        return ar;
      }, []);
      
      // 4. The text and text styles are set to the cells.
      for (let i = row - 1; i < table.getNumRows(); i++) {
        const t = table.getCell(i, column - 1).editAsText();
        const run = textRuns.shift();
        t.setText(run.text);
        run.styles.forEach((r, j) => {
          t.setBackgroundColor(j, j, r.backgroundColor);
          t.setForegroundColor(j, j, r.foregroundColor);
          t.setItalic(j, j, r.italic);
          if (r.textAlignment) t.setTextAlignment(j, j, r.textAlignment);
        });
      }
      
      // 5. When the number of rows are smaller than the number of rows of splitted values, the new rows are appended and the text and text styles are set to the cells.
      while (textRuns.length > 0) {
        const appendedRow = table.appendTableRow();
        for (let i = 0; i < table.getRow(row - 1).getNumCells(); i++) {
          appendedRow.appendTableCell("");
        }
        const t = appendedRow.getCell(column - 1).editAsText();
        const run = textRuns.shift();
        t.setText(run.text);
        run.styles.forEach((r, j) => {
          t.setBackgroundColor(j, j, r.backgroundColor);
          t.setForegroundColor(j, j, r.foregroundColor);
          t.setItalic(j, j, r.italic);
          if (r.textAlignment) t.setTextAlignment(j, j, r.textAlignment);
        });
      }
    }
    
    函数myFunction(){
    //1.检索表。
    const body=DocumentApp.getActiveDocument().getBody();
    const table=body.getTables()[0];
    //2.检索表的单元格“B2”。
    const row=2;//请设置行号。
    const column=2;//请设置列号。
    const cell=table.getCell(第1行,第1列);
    //3.创建包含文本和文本样式的对象。用于将值拆分为单元格。
    const text=cell.editAsText();
    设temp=[];
    const textrunts=[].reduce.call(text.getText(),(ar,c,i,a)=>{
    如果(c!=“\n”)临时推送({text:c,foregroundColor:text.getForegroundColor(i),backgroundColor:text.getBackgroundColor(i),textAlignment:text.getTextAlignment(i),italic:text.isItalic(i)});
    如果(c==“\n”| i==a.length-1){
    ar.push({text:temp.reduce((s,{text})=>s+=text,“”),样式:temp});
    温度=[];
    }
    返回ar;
    }, []);
    //4.文本和文本样式设置为单元格。
    for(设i=row-1;i{
    t、 setBackgroundColor(j,j,r.backgroundColor);
    t、 setForegroundColor(j,j,r.foregroundColor);
    t、 setItalic(j,j,r.italic);
    if(r.textAlignment)t.setTextAlignment(j,j,r.textAlignment);
    });
    }
    //5.当行数小于拆分值的行数时,将追加新行,并将文本和文本样式设置为单元格。
    while(textrunts.length>0){
    const appendedRow=table.appendTableRow();
    for(设i=0;i{
    t、 setBackgroundColor(j,j,r.backgroundColor);
    t、 setForegroundColor(j,j,r.foregroundColor);
    t、 setItalic(j,j,r.italic);
    if(r.textAlignment)t.setTextAlignment(j,j,r.textAlignment);
    });
    }
    }
    
    结果: 当为初始表运行上述脚本时,可以获得以下结果。在本演示中,首先展开单元格“B2”的值,然后展开单元格“C3”的值

    注:
    • 此示例脚本是为上述情况准备的。如果更改了上述图像的规格,则可能无法使用该脚本。因此,请注意这一点。
    • 在此示例脚本中,
      BackgroundColor
      foreggroundcolor
      用作文本样式。当您想使用其他文字样式时,请修改上述脚本
    参考资料:

    这可能有些用处:谢谢Cooper提供的信息!可能saveCellText1(cObj)函数与我的问题有关?我会试着把剧本通读一遍。
    function myFunction() {
      // 1. Retrieve table.
      const body = DocumentApp.getActiveDocument().getBody();
      const table = body.getTables()[0];
      
      // 2. Retrieve the cell "B2" of the table.
      const row = 2;  // Please set the row number.
      const column = 2;  // Please set the column number.
      const cell = table.getCell(row - 1, column - 1);
      
      // 3. Create an object including the text and text styles. This is used for splitting values to the cells.
      const text = cell.editAsText();
      let temp = [];
      const textRuns = [].reduce.call(text.getText(), (ar, c, i, a) => {
        if (c != "\n") temp.push({text: c, foregroundColor: text.getForegroundColor(i), backgroundColor: text.getBackgroundColor(i), textAlignment: text.getTextAlignment(i), italic: text.isItalic(i)});
        if (c == "\n" || i == a.length - 1) {
          ar.push({text: temp.reduce((s, {text}) => s += text, ""), styles: temp});
          temp = [];
        }
        return ar;
      }, []);
      
      // 4. The text and text styles are set to the cells.
      for (let i = row - 1; i < table.getNumRows(); i++) {
        const t = table.getCell(i, column - 1).editAsText();
        const run = textRuns.shift();
        t.setText(run.text);
        run.styles.forEach((r, j) => {
          t.setBackgroundColor(j, j, r.backgroundColor);
          t.setForegroundColor(j, j, r.foregroundColor);
          t.setItalic(j, j, r.italic);
          if (r.textAlignment) t.setTextAlignment(j, j, r.textAlignment);
        });
      }
      
      // 5. When the number of rows are smaller than the number of rows of splitted values, the new rows are appended and the text and text styles are set to the cells.
      while (textRuns.length > 0) {
        const appendedRow = table.appendTableRow();
        for (let i = 0; i < table.getRow(row - 1).getNumCells(); i++) {
          appendedRow.appendTableCell("");
        }
        const t = appendedRow.getCell(column - 1).editAsText();
        const run = textRuns.shift();
        t.setText(run.text);
        run.styles.forEach((r, j) => {
          t.setBackgroundColor(j, j, r.backgroundColor);
          t.setForegroundColor(j, j, r.foregroundColor);
          t.setItalic(j, j, r.italic);
          if (r.textAlignment) t.setTextAlignment(j, j, r.textAlignment);
        });
      }
    }