Transfer data from current worksheet to fresh workbook in Script Lab

I am exploring how to utilize TypeScript in Script Lab for Excel to duplicate the active worksheet into a brand new workbook. I came across a sample script that duplicates the worksheet within the same workbook, but I am unsure of how to modify it to copy the worksheet into a completely new workbook instead. Any thoughts or suggestions?

$("#setup").click(() => tryCatch(setup));
$("#copy-worksheet").click(() => tryCatch(run));

async function run() {
  await Excel.run(async (context) => {
    let myWorkbook = context.workbook;
    let sampleSheet = myWorkbook.worksheets.getActiveWorksheet();
    let copiedSheet = sampleSheet.copy("End");

    sampleSheet.load("name");
    copiedSheet.load("name");

    await context.sync();

    console.log("'" + sampleSheet.name + "' was copied to '" + copiedSheet.name + "'");
  });
}

async function setup() {
  await Excel.run(async (context) => {
    context.workbook.worksheets.getItemOrNullObject("Sample").delete();
    const sheet = context.workbook.worksheets.add("Sample");

    let expensesTable = sheet.tables.add("A1:E1", true);
    expensesTable.name = "SalesTable";

    expensesTable.getHeaderRowRange().values = [["Product", "Qtr1", "Qtr2", "Qtr3", "Qtr4"]];

    expensesTable.rows.add(null, [
      ["Frames", 5000, 7000, 6544, 4377],
      ["Saddles", 400, 323, 276, 651],
      ["Brake levers", 12000, 8766, 8456, 9812],
      ["Chains", 1550, 1088, 692, 853],
      ["Mirrors", 225, 600, 923, 544],
      ["Spokes", 6005, 7634, 4589, 8765]
    ]);

    sheet.getUsedRange().format.autofitColumns();
    sheet.getUsedRange().format.autofitRows();

    sheet.activate();
    await context.sync();
  });
}

/** Default helper for invoking an action and handling errors. */
async function tryCatch(callback) {
  try {
    await callback();
  } catch (error) {
    // Note: In a production add-in, you'd want to notify the user through your add-in's UI.
    console.error(error);
  }
}

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

Angular2 and Typescript paired with Visual Studio 2013

Currently, I am utilizing the angular2 QUICKSTART and encountering an issue where Visual Studio fails to recognize Angular2 with typescript import Modules. However, everything else seems to be functioning correctly: https://i.stack.imgur.com/0s46Y.jpg Th ...

Modifying SASS variable within an Angular 2 TypeScript project

How can I update the Sass color variable based on user input in Angular 2? I came across a helpful resource, but it doesn't include any examples specifically for Angular 2. Any assistance would be greatly appreciated. Thank you! ...

Retrieving the latest status array by index using Typescript in Angular

Need help with this code logic. I am working on an array and function : import { Component } from '@angular/core'; @Component({ selector: 'my-app', templateUrl: './app.component.html', styleUrls: [ './app.compon ...

Unable to instantiate a class object in TypeScript by using the new operator

Object instantiation is occurring without any issues: let paginationDetails: Common.Models.PaginationModel = { PageNumber: this.pageNumber, PageSize: this.pageSize, SearchText: this.denominationFilter, Ascending: true }; However, when att ...

Handling Errors in RXJS Angular - Utilizing .subscribe and Observable Strategy

For creating a new product using a backend API, the Angular frontend code needs to make a call to the API. I am interested in learning how to implement error handling with the use of .subscribe method. Currently, I am utilizing HTTPClient along with Observ ...

Capture screenshots programmatically with selenium and VBA

I have been struggling to take a screenshot of a specific element on a webpage. Despite trying various lines of code, I have not been successful in capturing the image accurately. Here is the link to the website Below is the code snippet that I have attem ...

Using Java script with Selenium

When attempting to write the sum of two numbers dynamically in an excel sheet, I encountered a problem where only the last row is being written. I suspect that the issue may be related to the excel sheet closing without autosave and then reopening for the ...

injecting a variable from the configuration service into a TypeScript decorator

I am interested in setting up a scheduled task for my NestJs application to run at regular intervals. I found information on how to use intervals in the NestJs documentation. Since my application uses configuration files, I want to keep the interval value ...

"Creating a dynamic TreeList in Ignite UI by linking pairs of names and corresponding

I recently developed a drag and drop tree list inspired by the tutorial on IgniteUI website. The main tree list functions properly, but I encountered an issue with the child nodes displaying as undefined, as illustrated in the image below: https://i.sstat ...

What is the reason that {a: never} is not the same as never?

Is there a reason the code {a: never} cannot be simplified to just never? I believe this change would resolve the issues mentioned below. type A = {tag: 'A', value: number} type B = {tag: 'B', value: boolean} type N = {tag: never, valu ...

What is the optimal approach to utilize: @ViewChild or @Output?

When it comes to sharing data between two directly related components (parent-child), there are a few options available such as @ViewChild and @Output. While @ViewChild provides more control and requires coding in the parent component, @Output involves cod ...

Implementing pagination for an Angular Material table using an HTTP request to set the page

How can I update the table page index when users click on next and previous buttons in an API that fetches data based on a specified Page number? It's important to note that I have already created a shared table. @Input() columns: Column[] = []; @In ...

Using aliases in npm packages is not supported

I am working on creating an npm package that I want to use in another application. During development, I set a path in tsconfig for importing various modules instead of using a relative path. However, when I download my package into the test app, it is una ...

Adding the activateRoute class to Angular for enhanced functionality

My question pertains to a specific section in the book pro-Angular-6, where I encountered the following syntax: constructor(private model:Model,activatedRoute:ActivatedRoute) {} I am unsure about the following aspects: How can we use a class without i ...

Mastering the utilization of custom input events in PrimeNG with Angular

I am currently working on an Angular 16 project. Within this project, I have implemented a number input field that is being validated using formControls. To make my work more efficient, especially since this input is used frequently, I decided to encapsula ...

Having trouble extracting primary color from image with Microsoft's Computer Vision

I've hit a roadblock with this dilemma that has been perplexing me for quite some time now. My goal is to determine the dominant color of an image using Microsoft's Computer Vision service. A snippet of my code can be seen below: import {VisualFe ...

Customize Angular Material's Mat-Dialog background blur/darkening effect

Greetings, dear community members, I am currently utilizing angular along with angular material in my projects. By default, when a material dialog is opened, it slightly darkens the background. However, I am interested in having a blurred background inst ...

The factory class is responsible for generating objects without specifying their type

I manage a factory that specializes in facilitating dependency injection. Here's an example of what it looks like: import SomeImportantObject from "./SomeImportantObject" import DataInterface from "./DataInterface" class NoodleFactory { this.depen ...

Angular 2 Typescript: Understanding the Structure of Member Properties and Constructors

I am currently exploring a project built with Ionic 2, Angular 2, and Typescript, and I find myself puzzled by the way member properties are being set. In the code snippet below, I noticed that Angular automatically injects dependencies into the construc ...

Validation in Angular2 is activated once a user completes typing

My goal is to validate an email address with the server to check if it is already registered, but I only want this validation to occur on blur and not on every value change. I have the ability to add multiple controls to my form, and here is how I have st ...