Here is what I have so far. I have an empty spread sheet. I'm not using your urls, simply making repeated calls to UrlFetchApp.fetch("http://www.google.com/");
. You can duplicate and try it out. If I knew the best way to test for urls[3] completion I can work it into the script but for now simply testing against the number of urls. I've added a new function repeatUrl. I can't test it because I'm really not sure of the response
that stops loop. But it should work. And because each google.script.run
is only about 1-2 min. you should not exceed the time limit. (I think).
In the app script I have:
In Code.gs:
function onOpen() {
var menu = SpreadsheetApp.getUi().createMenu("Fetch Urls");
menu.addItem("Side Bar","showSideBar");
menu.addToUi();
}
function include(filename) {
return HtmlService.createHtmlOutputFromFile(filename)
.getContent();
};
function showSideBar() {
try {
var html = HtmlService.createTemplateFromFile('HTML_Test').evaluate();
SpreadsheetApp.getUi().showSidebar(html);
}
catch(err) {
SpreadsheetApp.getUi().alert(err);
}
}
function fetchUrl(url) {
try {
var response = UrlFetchApp.fetch("http://www.google.com/");
console.log(response);
return response.getResponseCode();
}
catch(err) {
return "Error: "+err.message;
}
}
I have HTML_Test (HTML file):
<!DOCTYPE html>
<html>
<head>
<base target="_top">
</head>
<body>
<input type="button" value="Fetch Urls" onclick="fetchOnClick()">
<?!= include("JS_Test"); ?>
</body>
</html>
And finally I have JS_Test (HTML file):
<script>
var urls = [ 'https://www.testurl.com/link1=processing',
'https://www.testurl.com/link2=processing',
'https://www.testurl.com/link3=processing'];
function fetchUrl( index ) {
try {
google.script.run.withSuccessHandler(
function(response) {
alert("index= "+index+" response= "+response);
//if( response.includes("Error:") ) return;
index++;
if( index < urls.length ) {
fetchUrl( index );
}
}
).fetchUrl(urls[index]);
}
catch(err) {
alert("Error in fetchUrl: "+err);
}
}
function repeatUrl(url) {
try {
google.script.run.withSuccessHandler(
function(response) {
alert(" response= "+response);
if( respone.includes("Records imported") ) {
repeatUrl(url);
}
}
).fetchUrl(url);
}
catch(err) {
alert("Error in fetchUrl: "+err);
}
}
function fetchOnClick() {
try {
fetchUrl(0);
repeatUrl(url[3]);
}
catch(err) {
alert(err);
}
}
</script>