Need to list all the sites under a google domain using Google Apps Script

回眸只為那壹抹淺笑 提交于 2019-12-12 01:09:45

问题


I need to list all the sites under a google domain using Google Apps Script. I have written a small script, but it only returns the sites I have created:

function list_sites() {
  var doc = DocumentApp.create('SiteList1');
  var domain = 'test.com';
  var sites = SitesApp.getSites(domain);

  doc.appendParagraph("Top");
  doc.appendParagraph(sites);
  doc.appendParagraph("Bottom");

  doc.saveAndClose(); // Save and close the document
}

How do I get all the sites?


回答1:


You can use the getAllSites method.

SitesApp.getAllSites(domain, start, max)

I'm not sure why it's seemingly undocumented and not in the API doc but you can find references to it and I have production scripts that use it daily.




回答2:


The example Peter mentioned, did not work for me, see also this question: Why does SitesApp.getSites(myDomain) not work? But I found a solution which also might help you:

function list_sites() {
  var doc = DocumentApp.create('SiteList1');
  var domain = 'example.com';
  var site = SitesApp.getSite(domain);

  doc.appendParagraph("Top");
  var pages = site.getAllDescendants();
  for (i in pages) {
    doc.appendParagraph(pages[i].getTitle());
  }  

  doc.appendParagraph("Bottom");
  doc.saveAndClose(); // Save and close the document  
}



回答3:


https://gist.github.com/eduvik/46bb72f3ffea57402b3f

Needs to be run as an admin user

function listSites() {
  var domain = UserManager.getDomain();
  var PAGE_LENGTH=200;
  sheet = SpreadsheetApp.getActiveSheet();
  var sites = SitesApp.getAllSites(domain,0,PAGE_LENGTH);
  for(var i=0; sites.length != 0; i+=PAGE_LENGTH){
    for (var j=0; j<sites.length; j++) {
      sheet.appendRow([sites[j].getUrl()]);
    }
    sites = SitesApp.getAllSites(domain, i, PAGE_LENGTH);
  }
};


来源:https://stackoverflow.com/questions/12395695/need-to-list-all-the-sites-under-a-google-domain-using-google-apps-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!