Question

I have a form that inputs the form's data into a google spreadsheet. This data is listed in order of submission.

When i clear the content of the spreadsheet, the next data is inputted as if the other data existed, x number or rows down.

  1. data
  2. data

clear

  1. (empty)
  2. (empty)
  3. data

Here is the script I'm using to clear the content

function clear() {
  var sheet =   SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1IEcXuEtkwVmnOEeQE71YQcZTrSkFa1OjlP8JpXGonHk/edit#gid=2015797647').getSheetByName('Form Responses 1');
  var lastRow = sheet.getLastRow();
  var lastCol = sheet.getLastColumn();
  var range = sheet.getRange(2,2,lastRow,lastCol);
  range.clearContent();

Is there a different way i should be clearing or selecting the data to clear?

Or is there a way to delete rows that keeps the formatting of the spreadsheet?

Was it helpful?

Solution

You should simply delete the rows instead of clearing their content :

    function clear() {
  //    var sheet =   SpreadsheetApp.getActive().getSheetByName('Form Responses 1');
      var sheet =   SpreadsheetApp.openByUrl('https://docs.google.com/spreadsheets/d/1IEcXuEtkwVmnOEeQE71YQcZTrSkFa1OjlP8JpXGonHk/edit#gid=2015797647').getSheetByName('Form Responses 1');
      var lastRow = sheet.getLastRow();
      sheet.deleteRows(2,lastRow-1);
    }

if you want to keep the total number of available rows in your sheet (which is not really necessary since responses automatically insert new rows but who knows...) add this line at the end of the code

  sheet.insertRows(2,lastRow-1);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top