Begin composing in Excel

I am looking to manipulate an existing Excel file by writing values from an array and closing it all on the client side. Here is the code snippet I have been using:

for (var c=0; c<arrMCN.length; c++)
{
var mcnCreate = arrMCN[c];

var mcnNumber =mcnCreate.getProperty("agu2a_nb");
var mcnType=mcnCreate.getProperty("pdm_ecn_type");
var mcnProjectCode=mcnCreate.getProperty("pdm_project_code");
var mcnState=mcnCreate.getProperty("state");

var Excel = new ActiveXObject("Excel.Application");
Excel.Visible = true;
Excel.Workbooks.Open("C:\\test.xls");
Excel.Activesheet.Cells(1,1).Value = mcnNumber;
var data =  Excel.Activesheet.Cells(1,1).Value;
return data;

}

Encountering the "ActiveX component cannot create an object" error was a setback for me initially. However, after tweaking the security settings in the Internet options, the error was resolved.

After successfully writing just one value into the Excel file, my next goal is to figure out how to efficiently write all the data from the array into the spreadsheet.

Any insights or suggestions on this matter would be greatly appreciated!

Answer №1

   var Excel = new ActiveXObject("Excel.Application");
   Excel.Visible = true;
   Excel.Workbooks.Open("C:test.xls");
   var excel_sheet = excel_file.Worksheets("Sheet11");
   var data = excel_sheet.Cells(1,1).Value = create;
   return data;

Make sure the URL was called properly:C:test.xls You can confirm using some Alert Function

Answer №2

Below is the effective solution I found for my query.

let ExcelApp = new ActiveXObject("Excel.Application");
let fileSystemObj = new ActiveXObject("Scripting.FileSystemObject");
let checkFileExistence = fileSystemObj.FileExists("C:\\Report.xlsx");

if(checkFileExistence) {
    fileSystemObj.DeleteFile("C:\\Report.xlsx", true);
}

let ExcelSheet = new ActiveXObject("Excel.Sheet");
let offset = 0;
let row = 2;

for (let index = 0; index < arrToExcel.length; index++) {
    let createExcel = arrToExcel[index];
    let NumberValue = createExcel.getProperty("number");
    let TypeValue = createExcel.getProperty("type");
    let CodeValue = createExcel.getProperty("code");

    ExcelSheet.ActiveSheet.Range("A" + (row + 1 + offset)).Value = NumberValue;
    ExcelSheet.ActiveSheet.Range("B" + (row + 1 + offset)).Value = TypeValue;
    ExcelSheet.ActiveSheet.Range("C" + (row + 1 + offset)).Value = CodeValue;

    row += 1;
}

offset += 1;
ExcelApp.DisplayAlerts = false;
ExcelSheet.SaveAs("C:\\EcnExcelReport.xlsx");
ExcelSheet.Application.Quit();
ExcelApp.DisplayAlerts = true;

A big thank you to everyone who provided suggestions.

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

What is the best way to remove a specific HTML section using a JavaScript function?

I am struggling to figure out how to remove a specific HTML section using a button that is contained within the section itself. The section I want to delete was initially added by clicking a different button. Although I can successfully add a new section ...

Secure your application with Express.js and MySQL user authentication

Greetings everyone, I've been working on setting up a registration form using express.js and mysql. Unfortunately, it seems like my routes are not functioning correctly as they should. Even when all the required details are filled in correctly, the pr ...

several javascript onclick event listeners for manipulating the DOM

I am currently experimenting with d3 and attempting to create a simple webpage to showcase my d3 examples for future reference. The page displays the output of the d3 code (specifically, the force layout from Mike Bostock) and then embeds the correspondin ...

Having difficulty setting up multiple buttons to share the same function in jQuery using HTML

After clicking a button, my code dynamically adds content to a div and inserts buttons with names like "teamReq_"+index+"_AddYear" into the document (where index is a number retrieved from a hidden input field). If these buttons are spammed, multiple divs ...

Error rendering {message} object on the Chrome Console

In my ReactJS component, I am utilizing the {message} parameter from props. Check out the code snippet below: import React from "react"; const MyMessage = ({ message }) => { if (message?.attachments?.length > 0) { return ( < ...

Obtain the ID of the textarea element when it is clicked

Is there a way to retrieve the id of a textarea that was focused when another element is clicked? I have tried using $(':input:focus').attr('id'), but the textarea quickly loses focus after the click, making it impossible to obtain the ...

Leveraging ForEach to merge two arrays and generate a fresh entity

I am in search of my final result which should be as follows: result = [{x: '12/12', y: 90 }, {x: '12/11', y: 0}, {x: '12/10', y: 92}, {x: '12/9', y: 0}, ... ] Currently, I have two arrays to work with. The first a ...

Sticky header in React data grid

Is there a way to implement a sticky header for a data grid in react? I have tried various methods but haven't been able to figure it out. Any suggestions would be appreciated. You can find my code sandbox example here. #react code export const Styl ...

Updating props in a recursive Vue 3 component proves to be a challenging task

I am facing an issue with two recursive components. The first component acts as a wrapper for the elements, while the second component represents the individual element. Wrapper Component <template> <div class="filter-tree"> &l ...

How to use AJAX to retrieve the text content of an HTML option value?

I have a script that displays a list of values, which I want to write: <option th:each = "iName : ${iNames}" th:value = "${iName}" th:text = "${iName}" th:selected="${selectedIName == iName}" In addition, I have the function setSelectedName in my .j ...

Tips for clearing Nightwatch session storage efficiently

To ensure the pop-up functionality was working properly, I had to reset the browser's session storage. By doing this, the pop-up will reappear. Is there a way to clear the page's Session Storage in Nightwatch? ...

Animate CSS with Javascript in reverse direction

Forgive me if this is a silly question, but I'm having trouble. I need a slide-in navigation menu for smaller screens that is triggered by JavaScript. Here is what I currently have: HTML <nav class="responsive"> <ul class="nav-list unstyl ...

Steer clear of utilizing Number() when working with scientific notation

I am attempting to perform the following task Number("0.00000000000122") yields 1.22e-12 However, my goal is to convert that number from a String to a Number. console.log(Number("0.00000000000122")) ...

The strict mode in React reveals unexpected changes in state with double rendering

While working with React, I've noticed that it renders twice in Strict mode during development to ensure the function remains pure without any side effects. However, I'm facing some inconsistency in retrieving the state in my case. It seems like ...

Transitioning menus in Ionic 2

I followed the instructions in the Ionic 2 menu documentation and tried to display the menu in a specific way: https://i.sstatic.net/zzm8f.png My intention was to have the menu displayed below the content page while keeping the menu button visible. Howe ...

Django Ajax filter displaying issue on HTML page

I'm uncertain about the correctness of my Ajax implementation. When using Django's built-in tags, the objects I pass through Ajax are not appearing on my template HTML page. view_results.html <div> <input id="search" name="search" t ...

There seems to be an issue with the next function's functionality within a Nodejs middleware

Currently, I am delving into the world of Nodejs with expressjs. My focus is on understanding middleware functions and specifically, the role of "next". In the middleware concept, "next" simply moves on to the next middleware in line. So, what exactly is ...

What is the process of adding a div to the left side of the parent element in AngularJS

I am trying to append the code on the left side of the click. My current controller code looks like this: demo.$inject = ['$scope']; demo.directive("boxCreator", function($compile){ return{ restrict: 'A', l ...

I am having trouble executing a script as the steps I have followed are not yielding the desired results. I am wondering where I may have made a mistake

I am trying to execute a script provided by Maciej Caputa in response to this question: How to import CSV or JSON to firebase cloud firestore The objective is to utilize a JSON file for uploading data to Cloud Firestore. As a newcomer to running scripts, ...

AngularJS allows for the creation of 2D arrays using the $Index

Currently, I am working on a project using AngularJS that involves creating a spreadsheet from a 2D array generated by the ng-repeat function. As part of this project, I am developing a function to update the initial values of the array when users input ne ...