Google apps script 无法使用Google Apps脚本将所有者添加到Google站点

Google apps script 无法使用Google Apps脚本将所有者添加到Google站点,google-apps-script,google-sites,Google Apps Script,Google Sites,我编写了一个谷歌应用程序脚本,以便制作一个谷歌网站模板的副本,然后向该副本添加一个新的所有者。 我使用的函数是异步的,如文档所述: 复制是异步的,复制操作可能仍在进行中 即使已返回对该站点的引用 复制站点后,我想使用该功能向站点添加新的所有者,如下所示: var template = SitesApp.getSiteByUrl(TEMPLATE_URL); // Retrieve the template var copyOfTemplate = SitesApp.copySite(domain

我编写了一个谷歌应用程序脚本,以便制作一个谷歌网站模板的副本,然后向该副本添加一个新的所有者。 我使用的函数是异步的,如文档所述:

复制是异步的,复制操作可能仍在进行中 即使已返回对该站点的引用

复制站点后,我想使用该功能向站点添加新的所有者,如下所示:

var template = SitesApp.getSiteByUrl(TEMPLATE_URL); // Retrieve the template
var copyOfTemplate = SitesApp.copySite(domain, url, googleSiteName, summary, template);
copiedTemplateURL = copyOfTemplate.getUrl();
copyOfTemplate.addOwner(adminMail); // Add the customer as owner of the new site
脚本运行良好,没有错误,创建了Google站点,但没有添加所有者,有时,getUrl函数不会返回任何内容。我试图添加延迟实用程序。睡眠20秒,但它似乎不起作用,我认为这是一个肮脏的解决方案。
有人能想出解决这个问题的办法吗?谢谢

我终于想出了解决问题的办法

看起来,如果你复制的谷歌网站包含很多页面,那么这个副本的处理时间会更长,如果你在这段时间内试图添加一个所有者,那么它将无法工作。此外,如果在复制期间添加所有者后使用该函数,它将返回正确的所有者。但是,当网站完全复制时,共享设置中不会提及您添加的所有者,因此无法访问网站模板的副本

其思想是通过计算复制模板上的函数返回的子代页面数,等待复制完成,并将其与原始模板上的相同函数返回的页面数进行比较。然后,您可以安全地添加新的所有者

var timer = 1000;
var template = SitesApp.getSiteByUrl("https://sites.google.com/a/domain.ext/yourtemplate"); // Retrieve the template
var copyOfTemplate = SitesApp.copySite(domain, url, googleSiteName, summary, template);
var descendants = copyOfTemplate.getAllDescendants().length;
var numberOfPages = template.getAllDescendants().length;

while(descendants < numberOfPages || descendants === null) {
  Utilities.sleep(timer);
  timer = timer + 5000; // sort of "additionnal backoff"
  descendants = copyOfTemplate.getAllDescendants().length;
}
//At this point the copy should be completed
copyOfTemplate.addOwner("yourNewOwner@domain.ext"); // Add the customer as owner of the new site
我的模板包含41个子页面,正确复制大约需要35秒