csvgoogle-apps-scriptgoogle-sheetscsv-import

Import updated column only google script


I have a CSV file that contains employee names in column A and targets in column B. I have the script to import the data into a google sheet each day. However, I only want to import column B when the trigger runs so that the sheet populates a new column with the daily updated values without adding column A.

function importCSVFromGoogleDrive() {
  var ss = SpreadsheetApp.getActiveSpreadsheet();
  var file = DriveApp.getFilesByName("Employee Targets.csv").next();
  var csvData = Utilities.parseCsv(file.getBlob().getDataAsString().split('\r').splice(1)) ;
  var sheet = SpreadsheetApp.getActive().getSheetByName("Daily Tracker");
  var lastcol = sheet.getLastColumn();
  sheet.getRange(1, lastcol + 1, csvData.length, csvData[0].length).setValues(csvData);
  sheet.getRange(1,sheet.getLastColumn()).setValue(Utilities.formatDate(new Date(), "your_timezone", "dd-MM-yy")); 
}

Solution

  • You'll need to create a new array from csvData with just the values of column B.

      var outputArray = []
      for (var row in csvData) {
        outputArray.push([csvData[row][1]]); // to post just column 2 to the sheet
      }
    

    And then post the data in the outputArray to the sheet instead of csvData.


    PS:

    -- As arrays are zero-based, column 2 values are in csvData[row][1].

    -- If csvData is likely to be null, you may want to wrap it in an if statement to avoid errors.