问题
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