How to loop a google spreadsheet column values and set result in column B?

前端 未结 1 1451
无人共我
无人共我 2021-01-23 07:06

i have a list of urls in column A and i want to loop this column A and set the status code for each url to column B. I tried this code but i keep getting Range not foun

1条回答
  •  有刺的猬
    2021-01-23 07:52

    There are probably better (and more elegant) ways to do it, but at least this seems to work:

     function getStatus() {    
    
      var queryString = Math.random();    
    
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("Sheet1");
    
      var lastRow = sheet.getLastRow();
      var lastColumn = sheet.getLastColumn(); 
      var dataRange = sheet.getRange(2, 1, lastRow -1, lastColumn);
      var data = dataRange.getValues();
      var row = 0;
      var len = data.length;
    
    
      for (row; row < len; row++) {
        if (data[row] != '') {    
    
            var cellFunction1 = '=HTTPResponse("' + data[row][0] + '?' + queryString + '")';
            // change the following line, last number for another column
            var cell = sheet.getRange(row+2, 2);
            cell.setValue(cellFunction1);
    
         } 
      }    
    
      }    
    
    function HTTPResponse( uri )
    {
     var response_code ;
    try {
     response_code = UrlFetchApp .fetch( uri ) .getResponseCode() .toString() ;
     }
    catch( error ) {
     response_code = error .toString() .match( / returned code (\d\d\d)\./ )[1] ;
     }
    finally {
     return response_code ;
     }
    }
    

    Maybe you can go from here and improve it. screenshot spreadsheet columns

    0 讨论(0)
提交回复
热议问题