0
votes

I wanted to read data from the webpage for this I have used the Importdata function. For auto-refresh wrote a script. Below is the code. I am getting error like TypeError: Cannot read property 'getRange' of null (line 16, file "Code") How to get rid of this error.

function getData() {
  
  var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("2020 MTD");
  var queryString = Math.random();
  var cellFuntion = '=IMPORTDATA("https://dosairnowdata.org/dos/historical/NewDelhi/2020/NewDelhi_PM2.5_2020_05_MTD.csv")';
     
     sheet.getRange('A1').setValue(cellFuntion);
    
     
}
2

2 Answers

3
votes

The documentation for getSheetByName says, "Returns null if there is no sheet with the given name." Therefore, you probably don't have a sheet named "2020 MTD." Create it or correct the name.

0
votes

Below is the working code.

 function importCSVFromWeb() {
    
      // Provide the full URL of the CSV file.
      var csvUrl = "https://dosairnowdata.org/dos/historical/NewDelhi/2020/NewDelhi_PM2.5_2020_05_MTD.csv";
      var csvContent = UrlFetchApp.fetch(csvUrl).getContentText();
      var csvData = Utilities.parseCsv(csvContent);
    
      var sheet = SpreadsheetApp.getActiveSheet();
      sheet.getRange(1, 1, csvData.length, csvData[0].length).setValues(csvData);
    }