每次都应该获取不同的Url

时间:2014-10-01 08:44:30

标签: google-apps-script

我想运行一个应用程序脚本来获取10个URL而不是同时进行,这意味着每个小时内该脚本将运行,并且在每次运行中它将从电子表格中选择一个包含10个URL和当指针到达第10个URL时,它再次从头开始。

请帮我解决这个问题。

1 个答案:

答案 0 :(得分:1)

电子表格可以记录已提取的URL。您可以将值放在单元格旁边的单元格中,并使用具有某个值的url。你可以输入" true"或1或"是"或者"取出",任何东西。

Which URL fetched

设置触发器

Current Triggers enter image description here

代码:

/**
 * Retrieves all the rows in the active spreadsheet that contain data and logs the
 * values for each row.
 * For more information on using the Spreadsheet API, see
 * https://developers.google.com/apps-script/service_spreadsheet
 */
function fetchURL_EveryHr() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
 var sheet = ss.getSheets()[1];
 var range = sheet.getRange(1, 1, 10, 3);
 var values = range.getValues();

 // Print values

 for (var row in values) {
   for (var col in values[row]) {
     Logger.log(values[row][col]);
   }
 }


 for (var row in values) {

   Logger.log('values[row][2]: Row: ' + row + " : " + values[row][2]);
   Logger.log(values[row][1]);
   Logger.log('Column A: ' + values[row][0]);

   if (values[row][2] != "fetched") {
      Logger.log('It found a URL that has not been fetched!  On row:  ' + values[row][0]);

      //Get the URL
      var urlToFetch = values[row][1];

      Logger.log('urlToFetch: ' + urlToFetch);

      //Fetch the URL
      var fetchContentsData = UrlFetchApp.fetch(urlToFetch);
      // The code below logs the HTML code of the URL.

      Logger.log('fetchContentsData: ' + fetchContentsData);

      //Add a value to the cell
       var rowToSet = values[row][0];
       Logger.log('rowToSet: ' + rowToSet);

       var cellToSet = "C" + rowToSet;
       Logger.log('cellToSet: ' + cellToSet);

       var cell = sheet.getRange(cellToSet);
       cell.setValue("fetched");

      //Quit
      return;
   };
 };

 //If code gets to this point, all URL's have been fetched.  Start over again
 //Delete all the values in column C

   var ColC = sheet.getRange("C1:C10");
       ColC.setValue("");

};