Google Spreadsheet doesn't start at top when responses are cleared

℡╲_俬逩灬. 提交于 2019-12-11 08:09:29

问题


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?


回答1:


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);


来源:https://stackoverflow.com/questions/25023382/google-spreadsheet-doesnt-start-at-top-when-responses-are-cleared

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!