如何编写脚本以在 Google 文档(google 应用程序)中启用链接?

How to write a script to enable links in Google Docs (google apps)?

谁能帮我解答一下?
我可以用脚本编写 link 到 Google 文档,但我不知道如何 link URL.
我想在以下示例中启用 link "tmpURL"。

  var rowsData =  [
                    ['titleA', 'URL'], 
                    ['AAA', tmpURL]
                  ];

  table = body.appendTable(rowsData);
  table.getRow(0).editAsText().setBold(true);

谢谢。

我相信你的目标如下。

  • 您想要附加一个 table 并且您想要将超链接设置到 table 的“B”列。 URL 与单元格中的文本相同。
  • 您想使用 Google Apps 脚本实现此目的。

这样的话,下面的修改怎么样?

修改后的脚本:

var body = DocumentApp.getActiveDocument().getBody();
var tmpURL  = "###"; // Please set the URL.

var rowsData = [
  ['titleA', 'URL'],
  ['AAA', tmpURL],
];
table = body.appendTable(rowsData);
table.getRow(0).editAsText().setBold(true);
var text = table.getCell(1, 1).editAsText();
text.setLinkUrl(text.getText());
  • 在此修改中,第 2 行的“B”列具有超链接。

  • 当你想反映“B”栏的超链接时,也可以用下面的脚本。在此脚本中,即使行数超过 2,“B”列也有超链接。

      var body = DocumentApp.getActiveDocument().getBody();
      var tmpURL  = "###"; // Please set the URL.
    
      var rowsData = [
        ['titleA', 'URL'],
        ['AAA', tmpURL],
      ];
      table = body.appendTable(rowsData);
      table.getRow(0).editAsText().setBold(true);
      var rows = table.getNumRows();
      for (var r = 1; r < rows; r++) {
        var text = table.getCell(r, 1).editAsText();
        text.setLinkUrl(text.getText());
      }
    

参考文献: