By using this site, you agree to the Privacy Policy and Terms of Use.
Accept
rocoderesrocoderes
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Notification Show More
Latest News
How to set the dropdown value by clicking on a table row
Javascript – How to set the dropdown value by clicking on a table row
JavaScript
Attempting to increase the counter, when the object's tag exist
Javascript – Attempting to increase the counter, when the object’s tag exist
JavaScript
Cycle2 JS center active slide
Javascript – Cycle2 JS center active slide
JavaScript
Can import all THREE.js post processing modules as ES6 modules except OutputPass
Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass
JavaScript
How to return closest match for an array in Google Sheets Appscript
Javascript – How to return closest match for an array in Google Sheets Appscript
JavaScript
Aa
Aa
rocoderesrocoderes
Search
  • Home
  • HTML & CSS
    • Login and Registration Form
    • Card Design
    • Loader
  • JavaScript
  • Python
  • Internet
  • Landing Pages
  • Tools
    • Google Drive Direct Download Link Generator
    • Word Count
  • Games
    • House Painter
Follow US
High Quality Design Resources for Free.
rocoderes > JavaScript > Javascript – Delete all columns inside a Column Group with Google Apps Script
JavaScript

Javascript – Delete all columns inside a Column Group with Google Apps Script

Admin
Last updated: 2024/01/07 at 6:58 AM
Admin
Share
2 Min Read
Delete all columns inside a Column Group with Google Apps Script

Problem:

I’m trying to delete all columns inside Column Groups in all of my Spreadsheet Sheets, but I don’t know how to manage with Column Groups. Each sheet has Column Groups in different (random) ranges, so I can’t get the Groups by it’s concrete range, I’m looking for a way to get the ranges of Column Groups and then delete the columns of the range or something like that.

Contents
Problem:Solution:Sample script 1:Sample script 2:Note:References:
function deleteColumnsInsideColumnGroup(fileSheets) {
  for (var i = 0; i < fileSheets.length; i++) {
    var sheet = destinationSheets[i];

  }
}

Thanks!

Solution:

I believe your goal is as follows.

  • You want to delete all columns of the column groups of all sheets in a Google Spreadsheet.
  • You want to achieve this using Google Apps Script.

In this case, how about the following sample script?

Sample script 1:

function myFunction() {
  SpreadsheetApp
    .getActiveSpreadsheet()
    .getSheets()
    .forEach(sheet => {
      const maxColumn = sheet.getMaxColumns();
      for (let c = maxColumn; c >= 1; c--) {
        const d = sheet.getColumnGroupDepth(c);
        if (d > 0) {
          sheet.deleteColumn(c);
        }
      }
    });
}
  • When this script is run, all columns of all column groups in all sheets of a Google Spreadsheet are removed.

Sample script 2:

If you want to reduce the process cost of the script, how about using Sheets API as follows? In this case, please enable Sheets API at Advanced Google services.

function myFunction() {
  const ss = SpreadsheetApp.getActiveSpreadsheet();
  const ssId = ss.getId();
  const { sheets } = Sheets.Spreadsheets.get(ssId, { fields: "sheets.columnGroups" });
  const requests = sheets.reduce((ar, { columnGroups }) => {
    if (columnGroups) {
      ar = [...ar, ...columnGroups.filter(({ depth }) => depth == 1).map(deleteDimension => {
        delete deleteDimension.depth;
        return { deleteDimension };
      })];
    }
    return ar;
  }, []).reverse();
  if (requests.length == 0) return;
  Sheets.Spreadsheets.batchUpdate({ requests }, ssId);
}

Note:

  • This script removes the columns from the sheets. So, I would like to recommend testing this script using a sample Spreadsheet.

References:

  • getColumnGroupDepth(columnIndex)
  • deleteColumn(columnPosition)
  • Method: spreadsheets.get
  • Method: spreadsheets.batchUpdate

Related

Subscribe to Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Share this Article
Facebook Twitter Email Print
What do you think?
Love0
Sad0
Happy0
Sleepy0
Angry0
Dead0
Wink0
Previous Article Updating Elements Within a Custom HTML Component JS Javascript – Updating Elements Within a Custom HTML Component JS
Next Article How can I overwrite a JS function added to core.action_registry in Odoo 15? Javascript – How can I overwrite a JS function added to core.action_registry in Odoo 15?
Leave a comment Leave a comment

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

- Advertisement -

You Might Also Like

How to set the dropdown value by clicking on a table row

Javascript – How to set the dropdown value by clicking on a table row

February 11, 2024
Attempting to increase the counter, when the object's tag exist

Javascript – Attempting to increase the counter, when the object’s tag exist

February 11, 2024
Cycle2 JS center active slide

Javascript – Cycle2 JS center active slide

February 10, 2024
Can import all THREE.js post processing modules as ES6 modules except OutputPass

Javascript – Can import all THREE.js post processing modules as ES6 modules except OutputPass

February 10, 2024
rocoderesrocoderes
Follow US

Copyright © 2022 All Right Reserved By Rocoderes

  • Home
  • About us
  • Contact us
  • Disclaimer
Join Us!

Subscribe to our newsletter and never miss our latest news, podcasts etc.

Zero spam, Unsubscribe at any time.
Welcome Back!

Sign in to your account

Lost your password?