当前位置: 动力学知识库 > 问答 > 编程问答 >

Check Google site if page name already exists when uploading folders

问题描述:

I am uploading folders to a created google site using appscript, but sometimes an error occures during which the code is uploading the filenames to the site as a parentpage and does not finish uploading all hte file names. So, when I restart the code it will throw an error saying that a page with that name already exists. Is there a way to do an if statement around the create page portion of code so that it checks to see if the name already exists. the code is provided below,

while (folders.hasNext()) {

var folder = folders.next();

if (folder.getName() == "Work_Orders(Evadale)3"){

Logger.log("I made it into the uploads folder");

var subFolders = folder.getFolders();

while(subFolders.hasNext()){

var subFolder = subFolders.next();

var files = subFolder.getFilesByType('text/html');

var stringChange = subFolder.getName();

var parentname = stringChange.replace(" ","-");

for (var c = 0; c<5; c++){

parentname = parentname.replace(" ","-");

parentname = parentname.replace("#","_");

parentname = parentname.replace("(","-");

parentname = parentname.replace(")","-");

parentname = parentname.replace("&","and");

}

Logger.log(parentname);

Logger.log("About to enter the if statement");

var pageParent = site.createWebPage(parentname, parentname, "");

var counter = 0;

while (files.hasNext()){

Logger.log(maxFiles);

var file = files.next();

var html = file.getBlob();

var datas = html.getAs('text/plain');

var infoString = datas.getDataAsString(); //this has a quota, see if this can be done some other way?

var name = file.getName();

Logger.log(name);

name = name.replace(".html","");

cache.put("parent"+maxFiles,parentname);

cache.put("child"+maxFiles, name);

cache.put("infoString"+maxFiles, infoString);

maxFiles++;

}

Logger.log("Counter"+counter);

Logger.log("maxFiles"+maxFiles);

Logger.log("Just exited the if statement");

cache.put("counter", counter);

}

}

cache.put("maxFiles", maxFiles);

cache.put("begin", begin);

cache.put("end",end);

}

网友答案:

Maybe you could use the getAllDescendants() method?

Google Documentation - getAllDescendants()

That would get you an array of descendant page names. Then you could use the JavaScript indexOf() method of the array name to check if the page name exists in the array.

JavaScript indexOf() method

分享给朋友:
您可能感兴趣的文章:
随机阅读: