PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Monday, August 29, 2022

[FIXED] How to Enclose an Entire Google Sheets .csv Column in Quotes?

 August 29, 2022     csv, excel, google-sheets     No comments   

Issue

I'm currently trying to enclose an entire column in a Google Sheets CSV file in double quotes for a script that I'm writing. I've attached a link for the sample CSV sheet below:

https://docs.google.com/spreadsheets/d/1qhXEG_IxBzjTDkq0r2L-9MTAM6iO1L7qDFJCnkcI4n4/edit?usp=sharing

When I export this sheet then the first cell in the 2nd row will be enclosed in quotations due to the special characters, but the second cell will not be as it does not have any special characters. For consistency when reading from a cell I want entries in the second column to be enclosed in double quotes.

I'm hoping for something simple such as changing a setting in the Google Sheets or potentially a function as I'm trying to make my script as dummy proof and simple for non-tech users. Is there any way to do this?


Solution

You can run this function before exporting the content of your sheets to enclose the entire 2nd column with double quotes:

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(1,2,sheet.getLastRow(),1); 
  var values = range.getValues();
  var quoted = values.map(x => x.map(y => '"' + y + '"'));
  
  range.setValues(quoted);
}

Screenshot:

enter image description here

UPDATED Ver:

I added a ternary operator which works if we'll be adding a single if-else like condition for arrow functions in javascript.

function myFunction() {
  var ss = SpreadsheetApp.getActiveSpreadsheet(); 
  var sheet = ss.getActiveSheet();
  var range = sheet.getRange(1,2,sheet.getLastRow(),1); 
  var values = range.getValues();
  var quoted = values.map(x => x.map(y => y[0] == '"' && y[y.toString.length] == '"' ? y : '"' + y + '"' ));
  range.setValues(quoted);
}


Answered By - Diego Sanchez
Answer Checked By - Cary Denson (PHPFixing Admin)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

1,206,048

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © 2025 PHPFixing