Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8856869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T14:27:19+00:00 2026-06-14T14:27:19+00:00

This is my very first day messing around with google script so excuse my

  • 0

This is my very first day messing around with google script so excuse my limitations.

I was trying to write a function that deleted all the rows in a Form Spreadsheet while leaving the header row behind.

Why does this work:

  // Loop through the data in the range and delete the rows
if (data.length > 1) {
  for (var row = 1; row < data.length; row++) {
    ws.deleteRow(row+1);
    }
    ws.deleteRow(2); 

 }

While the following:

if (data.length > 1) {
  for (var row = 1; row < data.length; row++) {
    ws.deleteRow(row+1);
    }

  }

Leaves row 2 behind

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T14:27:21+00:00Added an answer on June 14, 2026 at 2:27 pm

    Because the row indexes are 1-based (i.e., start at 1), you need to alter your for loop like this:

    for (var row = 1; row <= data.length; row++) {
    

    Assuming you have 2 rows, when you hit your second row in the loop (that is, when row = 2), the value of row will not be less than data.length (which is 2); it will be equal to the data.length. So with <, the test will fail and the code in the loop will not be executed that second time. With <= it will pass and code will be executed as desired.

    This same pattern will of course happen with any number of rows.

    EDIT: a few other problems:

    Because the removal will happen immediately, the rows will all shift after a removal and will be numbered differently. This can be handled in a couple ways, but here’s one: change your deletion to start from the end and work backwards. so, now we have:

    for (var row = data.length; row >= 1; row--) {
    

    Also, if you’re only trying to skip the first row, as the header row, rather that change the index used (e.g., row + 1), just change where you start/stop your loop. this is simpler and more understandable. so, the loop becomes:

     for (var row = data.length; row >= 2; row--) {
        ws.deleteRow(row);
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to write my very first python script. This was working but
This is a very basic question on Java. I've read somewhere that at first
At first, i have to say that this topic is very common topic. I
It is my first day to perl, and I find this warning very confusing.
This is my first day using c#. I think it is very easy :(.
This is my VERY FIRST Makefile and so I have cut and paste junk
I'm going a little mad I think. This is the VERY first thing I
So this was one of my very first questions here, but I have a
I've been seeing this instruction as the very first line of numerous CSS files
This is very basic magento question i guess. I want to first get all

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.