How can I adjust the timeout or enhance my code for Excel Online's ExcelScript error regarding the Range getColumn function timing out?

I am struggling with a code that is supposed to scan through the "hello" sheet and remove any columns where the top cell contains the letter B:

function main(workbook: ExcelScript.Workbook) {

  let ws = workbook.getWorksheet("hello");
  let usedrange = ws.getUsedRange(true);
  let qtycol = usedrange.getColumnCount() - 1;
  let values = usedrange.getValues();

  for (let j = qtycol; j >= 0; j--) {

    let command = values[1][j];

    if (command == "B") {
      usedrange.getColumn(j).delete(ExcelScript.DeleteShiftDirection.left);
    }
  }
}

Initially, this code worked fine on smaller files. However, as I started processing larger files, an error occurred:

Line 64: Range getColumn: Timeout

The problematic line seems to be related to the usedrange... instruction in line 64.

If anyone has suggestions for a more efficient approach or alternative solutions, I would greatly appreciate your feedback.

Thank you!

Answer №1

It seems that deleting a lot of columns at once in Excel may be causing the issue with updating the worksheet.

To work around this, you can create a multiple-area range (which is a read-only operation) and then delete it all in one go.

While I don't have access to ExcelScript, here is an equivalent VBA routine that accomplishes this:

Public Sub DeleteColumns()

    Dim J As Integer
    Dim Command As String
    Dim qtycol As Integer
    Dim ws As Worksheet
    Dim UsedRange As Range
    Dim toDeleteRange As Range
    
    Set ws = Application.ActiveWorkbook.Worksheets("hello")
    
    Set UsedRange = ws.UsedRange
    Let qtycol = UsedRange.Columns.Count - 1
    
    For J = qtycol To 1 Step -1 
    
        Let Command = UsedRange(1, J)
    
        If (Command = "B") Then
            If (toDeleteRange Is Nothing) Then
                Set toDeleteRange = UsedRange.Columns(J)
            Else
                Set toDeleteRange = Union(toDeleteRange, UsedRange.Columns(J))
            End If
        End If
    Next J
    
    toDeleteRange.Delete (XlDeleteShiftDirection.xlShiftToLeft)

End Sub

You can adapt similar methods using ExcelScript.

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Ways to retrieve the output parameter in the node mssql

` request.input('xyz',sql.Int,1); request.input('abc',sql.Numeric,2); request.output('pqr',sql.Int); request.output('def',sql.Char); request.execute('[StoredProcedure]',function(err,recor ...

Leveraging functions in a Node.js module

I am struggling with using a function inside a Node.js module. I have implemented a custom sorting function to sort an array of objects based on the value of a specific property. exports.getResult = function(cards) { cards.sort(sortByField('suit& ...

MUI: Transforming the uncontrolled value state of Select into a controlled one with a new component

I'm attempting to develop an edit form for modifying data fetched from a database based on its ID. Here is what I have tried: import React, {FormEvent, useEffect, useState} from "react"; import TextField from "@material-ui/core/ ...

Organizing objects into arrays in Node.js

I have an array and I need to concatenate an object after the array. Here is my array: const users = [ {name: "Joe", age: 22}, {name: "Kevin", age: 24}, {name: "Peter", age: 21} ] And here is my object: ...

When there are numerous websocket connections in Google Chrome, Socket.io can encounter issues and break down

I am encountering an issue where I create 60 client connections to a socket.io server using Google Chrome browser. The server sends screenshots to the clients at specific times, but some of the websocket connections, which are subprotocols of socket.io, ge ...

Tips for obtaining accurate response from axios

When utilizing axios, I receive my query response in the format of response.data.response.object. Is there a way to access the answer directly without going through response.data first? ...

What is the best way to retrieve the document DOM object within an EJS template?

I am attempting to display a list of participants when the user clicks on the button. However, every time I try, I encounter an error stating "document is not defined". (Please refrain from suggesting the use of jQuery!). <% var btn = document.getEle ...

Unlocking the potential of Vue within shadow dom environments

I am facing an issue with a shadow DOM that includes the root element and a Vue component. <template> <div class="container"> <div id="app"></div> </div> <script src="http://my-site.com/app/js/vue-compo ...

When utilizing PHP Form Validation alongside Javascript, the validation process does not halt even if a

I have been grappling with creating a basic HTML form validation using Javascript After experimenting with various examples, I am still facing the issue of my index page loading upon clicking the button on the form. Despite including "return false" in wha ...

What is the best way to incorporate a JavaScript function into an Angular 2 template?

I need a slider for my project and I am using AdminLTE <input type="text" value="" class="slider form-control" data-slider-min="-200" data-slider-max="200" data-slider-step="5" data-slider-orientation="horizontal" data-slider-sele ...

Crushing jQuery's Sortable/Droppable

Having a little issue here. I want to be able to toggle the sortable plugin's behavior by clicking a button - basically switching between sort mode and view mode. I've attempted to achieve this with the following code: function enterSortMode(){ ...

Is there a reason why the Chrome browser doesn't trigger a popstate event when using the back

JavaScript: $(document).ready(function() { window.history.replaceState({some JSON}, "tittle", aHref); $(window).bind("popstate", function(){ alert("hello~"); }); }); Upon the initial loading of the www.example.com page, the above JavaScript code is ex ...

Presenting JSON information in a concise and visually appealing manner

How can I extract a value from JSON data and display it in an HTML text field? The JSON data looks like this: {"name":"paul"}, but I only want to display "paul" in my text field. Here is my code in CodeIgniter PHP: $data['name'] = $this->name ...

The type 'Observable<any>' cannot be assigned to the type 'Observable<T>'

Here is the code I am working with: import {HttpClient} from '@ngular/common/http'; private httpClient: HttpClient; do_request(method: string, url: string, ...

Generating data types based on the output of functions

I'm currently working on optimizing my typescript react code by reducing repetition. I'm curious to know if there's a way to generate a type based on the return type of a known function? For example: const mapStateToProps = (state: StoreSt ...

Using an Angular route to trigger the resolution of data from a service

I'm having trouble figuring out how to implement Angular route resolve. The documentation does not provide much help for more complex aspects of the javascript framework like this. Here is the service I am using: app.service("AuthService", ["$http", ...

Why is my input field value not getting set by Angular's patchValue function

I've been attempting to populate an input field using the form group with patchValue(), but for some reason, the input remains empty. Here's a snippet of my code... component.html <form [formGroup]="createStoreForm" (ngSubmit)="createStor ...

Issue with Vue.js: routes are not found upon page refresh

Here is a basic vue-routing example: const Foo = { template: '<div>foo</div>' } const Bar = { template: '<div>bar</div>' } const routes = [ { path: '/foo', component: Foo }, { path: '/ba ...

Attempting to show the name in AngularJS

I've been working on mastering Angular JS, but I'm facing a challenge with displaying the user I just added to the database on the next page. While I can display other users, the newly added user's data just won't show up! I've tri ...

Issues with rendering of triangles in WebGL

My current project involves creating a webGL application for rendering randomly generated terrains. While the terrain rendering works smoothly, I've encountered an issue with rendering a simple quad to represent water - the triangles of the water are ...