【问题标题】:Impossible to add an owner to a Google Site using Google Apps Script无法使用 Google Apps 脚本将所有者添加到 Google 站点
【发布时间】:2014-07-08 04:42:09
【问题描述】:

我编写了一个 Google Apps 脚本来制作一个 Google 站点模板的副本,然后为这个副本添加一个新的所有者。 我使用copySite 函数,正如文档所述,它是异步的:

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

站点复制完成后,我想使用addOwner 函数为站点添加一个新所有者,就像这样:

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 秒的延迟(Utilities.sleep),但它似乎不起作用,我认为这是一个“肮脏”的解决方案。 有人能想到这种情况的解决方法吗?谢谢

【问题讨论】:

    标签: google-apps-script google-sites


    【解决方案1】:

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

    如果您复制的 Google 网站包含许多页面,则此副本的处理时间似乎会更长,如果您尝试在在此期间添加所有者,它将无法正常工作。此外,如果您在添加所有者后(在复制期间)使用getOwners() 函数,它将返回正确的所有者。但是,当网站被完全复制时,您添加的所有者不会在共享设置中提及,因此无法访问您网站模板的副本。

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

    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 秒才能正确复制。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-29
      • 1970-01-01
      相关资源
      最近更新 更多