1
votes

I need help with one small change, I have a script that will run daily in the morning and will display a report from a csv to a google sheet file and I have that working. my only problem is that for the report name I want the sheet name to be the current date of each day instead of having the way i have is just NEWDATA sheet name, I want current date added to the sheet name.

for example in this line var newsheet = ss.insertSheet("NEWDATA"); I don't want the newdata I want to grab the current date for the sheet name

example of the sheet name enter image description here

function importData() {
  var fSource = DriveApp.getFolderById("0ByXeCX01JfKJN1dTNk1SRlQyb1k"); // reports_folder_id = id of folder where csv reports are saved
  var fi = fSource.getFilesByName('201707160600070685.csv'); // latest report file
  var ss = SpreadsheetApp.openById("1T2JU4KwpJsnlJk0LOEZoHr9uqnNrVYwBWI1NxOwL4PU"); // data_sheet_id = id of spreadsheet that holds the data to be updated with new report data


      if ( fi.hasNext() ) { // proceed if "report.csv" file exists in the reports folder
        var file = fi.next();
        var csv = file.getBlob().getDataAsString();
        var csvData = CSVToArray(csv); // see below for CSVToArray function
        var newsheet = ss.insertSheet("NEWDATA"); // create a 'NEWDATA' sheet to store imported data
        // loop through csv data array and insert (append) as rows into 'NEWDATA' sheet
        for ( var i=0, lenCsv=csvData.length; i<lenCsv; i++ ) {
          newsheet.getRange(i+1, 1, 1, csvData[i].length).setValues(new Array(csvData[i]));
        }
        /*
        ** report data is now in 'NEWDATA' sheet in the spreadsheet - process it as needed,
        ** then delete 'NEWDATA' sheet using ss.deleteSheet(newsheet)
        */
        // rename the report.csv file so it is not processed on next scheduled run
        file.setName("report-"+(new Date().toString())+".csv");
      }
    };
1

1 Answers

1
votes

How about a following modification? Current date is retrieved using new Date(). The format of date is modified using Utilities.formatDate).

From :

var newsheet = ss.insertSheet("NEWDATA");

To :

var sheetname = Utilities.formatDate(new Date(), Session.getScriptTimeZone(), "MM/dd/yyyy");
var newsheet = ss.insertSheet(sheetname);

If I misunderstand your question, I'm sorry.