Simon GIS
Simon GIS

Reputation: 1055

Delete or Remove first row of Google Sheet Script export to CSV

I have this script in Google Sheets. Which export my selected sheets to a CSV

 var spreadsheet = SpreadsheetApp.getActiveSpreadsheet();
        var firstSheet = spreadsheet.getSheets()[0];
        var secondSheet = spreadsheet.getSheets()[2];
        
        spreadsheet.setActiveSheet(firstSheet).getRange("A2:AI19000").activate();
        
        
        function onOpen() {
          SpreadsheetApp.getUi()
            .createMenu('CSV')
            .addItem('Import from the file', 'userActionImportFromCSV')
            .addItem('Export to the file', 'userActionExportToCSV')
            .addToUi();
        }
        
        usp=sharing
        var CSV_FILE_ID = '1w';
        
    
        function userActionImportFromCSV() {
          var res = importFromCSV_(
            CSV_FILE_ID,
            SpreadsheetApp.getActive().getId(),
            SpreadsheetApp.getActiveSheet().getSheetId()
          );
          Logger.log(res);
        }
    
        function userActionExportToCSV() {
          var res = exportToCSV_(
            CSV_FILE_ID,
            SpreadsheetApp.getActive().getId(),
            SpreadsheetApp.getActiveSheet().getSheetId()
          );
          Logger.log(res);
        }
        
        function importFromCSV_(csvId, spreadsheetId, sheetId) {
          // Get CSV data
        
          var data = DriveApp.getFileById(csvId)
            .getBlob()
            .getDataAsString();
        
          var updateCellsRequest = Sheets.newUpdateCellsRequest();
          updateCellsRequest.fields = 'userEnteredValue';
          updateCellsRequest.range = { sheetId: sheetId };
          batchUpdateSpreadsheet_(
            {
              updateCells: updateCellsRequest,
            },
            spreadsheetId
          );
        
          var pasteDataRequest = Sheets.newPasteDataRequest();
          pasteDataRequest.coordinate = {
            sheetId: SpreadsheetApp.getActiveSheet().getSheetId(),
            rowIndex: 0,
            columnIndex: 0,
          };
          pasteDataRequest.data = data;
          pasteDataRequest.type = SpreadsheetApp.CopyPasteType.PASTE_VALUES;
          pasteDataRequest.delimiter = ',';
        
          var batchUpdateSpreadsheetResponse = batchUpdateSpreadsheet_(
            {
              pasteData: pasteDataRequest,
            },
            spreadsheetId
          );
          return batchUpdateSpreadsheetResponse;
        }
        
    
        function exportToCSV_(csvId, spreadsheetId, sheetId) {
          var url = Utilities.formatString(
            'httd=%s',
            spreadsheetId,
            sheetId
          );
        
          var data = UrlFetchApp.fetch(url, {
            headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken() },     muteHttpExceptions: true
          }).getBlob();
        
          DriveApp.getFileById(csvId).setContent(data.getDataAsString());
        }
        
        function batchUpdateSpreadsheet_(request, spreadsheetId) {
          var resource = {
            requests: [],
          };
          resource.requests.push(request);
          var batchUpdateSpreadsheetResponse = 

Sheets.Spreadsheets.batchUpdate(
                resource,
                spreadsheetId
              );
              return batchUpdateSpreadsheetResponse;
            }
    

I dont want to get the first row in my CSV export. So i start my get range at A2:AI19000. But still in my export Im getting the first row...

How i can fix this To get the export from second row of the sheet?

Or delete the first row of the export?

Upvotes: 2

Views: 999

Answers (1)

Tanaike
Tanaike

Reputation: 201388

I believe your goal as follows.

  • You want to export the values from the range of "A2:AI19000" on a sheet in a Spreadsheet as the CSV format using Google Apps Script.

Modification point:

  • Unfortunately, from your script, I'm not sure about the endpoint 'httd=%s' you are using. But from Utilities.formatString('httd=%s',spreadsheetId,sheetId), I guess that you might use the endpoint like https://docs.google.com/spreadsheets/d/%s/export?format=csv&gid=%s. From this, how about including the range in the endpoint? It's as follows.
    • https://docs.google.com/spreadsheets/d/%s/export?format=csv&gid=%s&range=%s

When above point is reflected to the Google Apps Script, it becomes as follows.

Sample script:

var spreadsheetId = "###";  // Please set the Spreadsheet ID.
var sheetId = "###";  // Please set the sheet ID.
var range = "A2:AI19000";  // Please set the range as a1Notation.

var url = Utilities.formatString('https://docs.google.com/spreadsheets/d/%s/export?format=csv&gid=%s&range=%s',spreadsheetId,sheetId,range);
var params = {headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken()}, muteHttpExceptions: true};
var data = UrlFetchApp.fetch(url, params);
console.log(data.getContentText())
  • By above script, the values are exported from the range of "A2:AI19000" on sheetId in spreadsheetId as the CSV format.

Note:

  • If your exportToCSV_ is modified it becomes as follows. In this case, when you call this function, please give range like exportToCSV_(CSV_FILE_ID,SpreadsheetApp.getActive().getId(),SpreadsheetApp.getActiveSheet().getSheetId(), "A2:AI19000");

      function exportToCSV_(csvId, spreadsheetId, sheetId, range) {
        var url = Utilities.formatString('https://docs.google.com/spreadsheets/d/%s/export?format=csv&gid=%s&range=%s',spreadsheetId,sheetId,range);
        var params = {headers: { Authorization: 'Bearer ' + ScriptApp.getOAuthToken()}, muteHttpExceptions: true};
        var data = UrlFetchApp.fetch(url, params).getBlob();
        DriveApp.getFileById(csvId).setContent(data.getDataAsString());
      }
    

Upvotes: 2

Related Questions