How to loop range of cells and set value in adjacent column

后端 未结 1 1545
眼角桃花
眼角桃花 2020-12-03 03:57

I\'m learning Google Apps Scripts for use with Google Spreadsheets.

I have a list of URLs in one column and I want to write a script to get the title element from e

相关标签:
1条回答
  • 2020-12-03 04:22

    May look something like this:

    function getTitles() {
      var ss = SpreadsheetApp.getActiveSpreadsheet();
      var sheet = ss.getSheetByName("url_list");
      var urls = sheet.getRange("G3:G").getValues();
      var titleList = [], newValues = [],
          response, doc, title;
    
      for (var row = 0, var len = urls.length; row < len; row++) {
        if (urls[row] != '') {
          response = UrlFetchApp.fetch(urls[row]);
          doc = Xml.parse(response.getContentText(),true);
          title = doc.html.head.title.getText();
          newValues.push([title]);  
          titleList.push(title);  
          Logger.log(title);
        } else newValues.push([]);
      }
    
      Logger.log('newValues ' + newValues);
      Logger.log('titleList ' + titleList);
    
      // SET NEW COLUMN VALUES ALL AT ONCE!
      sheet.getRange("H3").offset(0, 0, newValues.length).setValues(newValues);
      return titleList; 
    }
    
    0 讨论(0)
提交回复
热议问题