By implementing JavaScript formulas into a CSV file for importing into Google Sheets, the outcome is the creation of numerous columns

I have been struggling to insert the following formula into a single column in Google Sheets using JavaScript, but it keeps appearing in different columns due to the presence of commas.

=IF(A2="VALID", B2, 0)

Currently, I am utilizing the code provided below to create the CSV:

var CSVRowDemo = [];
var CSVDemo = 'data:text/csv;charset=utf-8,';
CSVRowDemo.push(['VALID/INVALID', 'Value', 'Check'])
let index = 2;
let check = '"' + '=IF(A'+index+'=\"VALID\"", B'+index+', 0)'.replace(/"/g, '""') + '"';
CSVRowDemo.push(['VALID', '100', check])
CSVRowDemo.forEach(function(rowArray) {
    let row = rowArray.join(',');
    CSVDemo += row + '\r\n';
});

encodedUri = encodeURI(CSVDemo);
link = document.createElement("a");
link.setAttribute("href", encodedUri);
link.setAttribute("download", "Basic.csv");
document.body.appendChild(link);
link.click();

My expected output resembles this (as seen in LibreOffice Calc): https://i.sstatic.net/TgkqC.png

However, in Google Sheets, it appears like this: https://i.sstatic.net/d0RU0.png

The closest solution I found is in this answer.

I am striving to achieve this solely with JavaScript, without relying on NodeJS or other libraries.

Your assistance is greatly valued.

Answer №1

Uncertain about the process of transferring the CSV data to Google Spreadsheet, would you be able to try out the suggested modification?

Initially:

let check = '"' + '=IF(A'+index+'=\"VALID\"", B'+index+', 0)'.replace(/"/g, '""') + '"';

Updated version:

let check = `"=IF(A${index}=""VALID"", B${index}, 0)"`;

or

let check = '"=IF(A'+index+'=""VALID"", B'+index+', 0)"';
  • Based on my experience, I can confirm that the modified CSV data can be successfully imported into Google Spreadsheet.

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

AngularJS | Dependency Injection appears to be silent and non-responsive

I've hit a roadblock in finding solutions to my query. In essence, I'm aiming to achieve dependency injection by linking my directive from the 'directives.js' file to be accessible in my controller within the 'controllers.js' ...

Reduce brightness in JavaScript - adjust downward

I stumbled upon this code snippet on Stack Overflow: function increase_brightness(hex, percent){ var r = parseInt(hex.substr(1, 2), 16), g = parseInt(hex.substr(3, 2), 16), b = parseInt(hex.substr(5, 2), 16); return '#' ...

Error 2300 in Vetur: Identical identifier found for '(Missing)'

Recently, I've been encountering a strange issue with Vetur in my typescript nuxt.js project. Every component, whether it's just an empty line or contains code, displays an error message on the first line. I can't pinpoint when this problem ...

How to decrypt a file with aes-256-cbc in node.js when the IV is undefined

Here are the encryption details: crypto.createHash('sha256').update('mySup3rC00lP4ssWord').digest() Initialization Vector: crypto.randomBytes(16) I followed the methods in this tutorial: https://medium.com/@brandonstilson/lets-enc ...

The multiplayer game is experiencing significant delays due to issues with the websocket server

While developing a multiplayer game, I have implemented a WebSocket Server to facilitate communication between clients. However, I am experiencing sporadic delays in message delivery from the server to the client. These delays can be significant, sometimes ...

Ways to create interaction between two web pages with JavaScript

I am attempting to design a webpage where one page can influence the content of another page. I have two HTML pages and a JavaScript file named "Controll.js" which contains a function that changes the content of "Indiv" in Index.html. This function is tr ...

Collect all the attribute values of the checkboxes that have been checked and convert them

Is there a way to retrieve the attribute values of all checked checkboxes as an XML string? <input type="checkbox" id="chkDocId1" myattribute="myval1"/> <input type="checkbox" id="chkDocId2" myattribute="myval43"/> <input type="checkbox ...

The CSS legend for a FLOT plot chart is being unexpectedly replaced

I am currently exploring the FLOT plot tool and facing difficulty with the legend display. It seems that the CSS styling for the legend is somehow being overridden in my code, resulting in an undesirable appearance of the legend: Even when I try to specif ...

Tips for capturing a jQuery trigger in traditional JavaScript

I am trying to trigger an event using jQuery and I want to bind to it in a non-jQuery script. It appears that while I can bind to the event in jQuery using on, I am unable to do so with addEventListener. Check out this jsFiddle for a demonstration: http: ...

Creating a visually appealing label by customizing it according to the child div

Can the label be styled based on whether the input is checked or not using CSS, or do I have to use JavaScript? <label class="filterButton"> <input name="RunandDrive" type="checkbox" value="1"> </label> ...

Using multiple GET methods on a single route in Express and Sequelize can lead to conflicts

I've created a simple product CRUD application with routes to search for products by ID and by name. However, when I send a request to http://localhost:4000/products?name=pen, the routes conflict with each other and I'm unable to retrieve the pro ...

Checking the time using jQuery

Looking for some guidance on using jQuery to validate a Time field. My goal is to restrict users from entering times before 18:00. I know HTML5 field validation can achieve this, but I find jQuery offers more flexibility in formatting user responses. Plus ...

"Learn the process of sending a post request using a combination of JQuery, Ajax

I am attempting to send a post request with the following code - var app = angular.module('myApp', []); app.controller('myCtrl', function ($scope) { $scope.data = {}; $scope.reqCalling = function () { ...

objects bound to knockout binding effects

I have been struggling to understand why the binding is not working as expected for the ‘(not working binding on click)’ in the HTML section. I have a basic list of Players, and when I click on one of them, the bound name should change at the bottom of ...

Discover how to utilize images encoded in base64 format within webhook embeds on Discord

Can someone help me with inserting an image into a Discord embed using a webhook? I have the image saved as a base64 string obtained from a database. However, my attempts so far have only resulted in an empty embed. https://i.sstatic.net/CVs4j.png const d ...

Is there a way to determine if a specific string matches a value within an object?

Within my codebase, there exists an object named "codes": var codes = { code1: 'test1' code2: 'test2' } I am looking to determine if this object possesses a specific property and then display the result in the console. if(inp ...

Issue with functionality of Bootstrap 4 Checkbox accordion

I am trying to achieve something similar to this example: https://getbootstrap.com/docs/4.0/components/collapse/#multiple-targets Instead of using a button, I want to use a checkbox. I want the collapse effect to occur when the checkbox is checked. < ...

Implementing beforeSend and complete in all instances of ajaxForm throughout the entire application as a universal

Is there a way to use the beforeSend and complete functions on all ajaxForms within a project without having to repeatedly insert the same code throughout the entire project? I have managed to achieve this by adding the following code for each form indivi ...

Utilizing the options object within JavaScript functions: A step-by-step guide

My current task involves gathering input as an argument along with some objects. function display(parameter) { var text = parameter.text; var element = parameter.element; document.getElementById(element).innerHTML = text; } As part of this task, I a ...

The function was called indirectly and the member appeared to be undefined

class Greetings{ constructor(user) { this.user = user; this.function_map = {"welcomeUser" : this.welcomeUser}; } welcomeUser(){ console.log(this.user); } } let g = new Greetings("Alice"); g.welcomeUser(); //=> o ...