如何使用 Apps 脚本生成带有嵌入式 AppScript 的 Google 文档?

发布于 2025-01-12 13:41:04 字数 1464 浏览 1 评论 0原文

我有兴趣使用我的库中的特定 Apps 脚本生成 Google 文档。 我创建文档的方式基本上是通过 Jeff Everhart 的代码看到的 此处

是否有可能更改代码,让新的 Google 文档从一开始就嵌入嵌入式 Apps 脚本?

我的简化代码:

  function createNewGoogleDocs() {
      const googleDocTemplate = DriveApp.getFileById('The template ID goes here');
      const destinationFolder = DriveApp.getFolderById('The destination folder ID goes here');

      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
      const rows = sheet.getDataRange().getValues();
  
  
    rows.forEach(function(row, index) {
      if (index === 0 ) return;
      if(row[rows[0].length - 1]) return;


      const copy = googleDocTemplate.makeCopy(`First Column Name (${row[0]})`, destinationFolder);
      const doc = DocumentApp.openById(copy.getId());
    
      // Hopefully here we can have the addition of the the Apps Script on the new Docs

      doc.saveAndClose();
      const url = doc.getUrl();
      sheet.getRange(index+1, rows[0].length).setValue(url);


    })

  }

I'm interested in generating a Google Document with a particular Apps Script from my library.
The way I have been creating the Documents has been through basically, Jeff Everhart's code as seen here.

Is it possible that the code be changed as to have the new Google Docs have an embedded Apps Script right from the get go?

My reduced code:

  function createNewGoogleDocs() {
      const googleDocTemplate = DriveApp.getFileById('The template ID goes here');
      const destinationFolder = DriveApp.getFolderById('The destination folder ID goes here');

      const sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');
      const rows = sheet.getDataRange().getValues();
  
  
    rows.forEach(function(row, index) {
      if (index === 0 ) return;
      if(row[rows[0].length - 1]) return;


      const copy = googleDocTemplate.makeCopy(`First Column Name (${row[0]})`, destinationFolder);
      const doc = DocumentApp.openById(copy.getId());
    
      // Hopefully here we can have the addition of the the Apps Script on the new Docs

      doc.saveAndClose();
      const url = doc.getUrl();
      sheet.getRange(index+1, rows[0].length).setValue(url);


    })

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心房的律动 2025-01-19 13:41:04

如果您的目标是创建绑定到文档的 Apps 脚本项目,则必须从该特定文档创建它。正如文档中所述:

如果满足以下条件,脚本就会绑定到 Google 表格、文档、幻灯片或表单文件:
它是根据该文档创建的,而不是作为独立脚本创建的。

绑定脚本附加到的文件称为“容器”。
绑定脚本通常表现得像独立脚本,除了
它们不会出现在 Google Drive 中,因此无法与 Google Drive 分离
他们绑定的文件,并且他们获得了一些特权
父文件。

如果您的最终目标是创建一个包含来自不同 Apps 脚本项目的嵌入式 Apps 脚本的文档,则必须首先创建该文档/脚本作为模板,然后使用 makeCopy 创建文档/脚本的副本。

我记录这个答案是为了帮助这个社区的其他人,如果您需要进一步的帮助,请随时发表评论。

If your goal is to create an Apps Script project bound to a Doc, you have to create it from that particular document. As it is said on the docs:

A script is bound to a Google Sheets, Docs, Slides, or Forms file if
it was created from that document rather than as a standalone script.

The file that a bound script is attached to is called a "container."
Bound scripts generally behave like standalone scripts except that
they do not appear in Google Drive, they cannot be detached from the
file they are bound to, and they gain a few special privileges over
the parent file.

If your end goal is to create a Doc with an embedded Apps Script from a different Apps Script project, you would have to first create that Doc/script as a template and then use makeCopy to create a copy of the Doc/script.

I am documenting this answer to help other people on this community, please feel free to leave a comment in your need further assistance.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文