How to set cells to plain text in google sheets

I've been grappling with a formatting issue that I'm hoping someone can assist me with. In my script, there's a point where I need to combine the date value (e.g., 11/20/2020) from one column with the time (3:00 PM) from another column. This combined value is then used to call the createEvent method of the calendar service class in order to create an event. The problem I'm encountering is that unless I manually format the cells in the spreadsheet using Format->Number->Plain Text, the concatenation results in nonsensical output.

concatenated dateStartTime is Wed Nov 20 2020 00:00:00 GMT-0500 (Eastern Standard Time) Sat Dec 30 1899 15:00:00 GMT-0500 (Eastern Standard Time)

and the cell(E4) will have value:

*Wed Nov 20 2020 00:00:00 GMT-0500 (Eastern Standard Time) Sat Dec 30 1899 15:00:00 GMT-0500 (Eastern Standard Time)*

This same issue occurs with concatenatedDateEndTime after running the script.

What I really need is to have the plain text "11/20/2020 3:00 PM" in the F4 cell where the concatenation takes place. Despite consulting the documentation and utilizing setNumberFormat("@"), none of the suggestions in this Post have proven effective.

Below is the relevant code snippet:

// Now we have to convert date,startTime and endTime var's to proper format to send to
    // Calendar service
    ss.getRange("B" + currentRowNumber).setNumberFormat("@");
    ss.getRange("F" + currentRowNumber).setNumberFormat("@");
    ss.getRange("G" + currentRowNumber).setNumberFormat("@");

    // Let's join date & start time and date & end time columns
    var concatenatedDateStartTime = eventDate + " " + startTime;
    var concatenatedDateEndTime = eventDate + " " + endTime;

    
    // Now lets create the calendar event with the pertinent data from the SS. See
    // https://developers.google.com/apps-script/reference/calendar/calendar-app#geteventsstarttime,-endtime
    eventCal.createEvent(
      summary,
      new Date(concatenatedDateStartTime),
      new Date(concatenatedDateEndTime),
      event
    );

I would like to streamline the process for users by allowing them to simply paste in a row of data without worrying about formatting the appropriate cells, as I aim to handle this programmatically.

Answer №1

  • To retrieve the string value of a specific cell, utilize the getDisplayValue() method.

  • If you wish to remove the formatting from a particular range, use clear().

Both functions belong to the range class, allowing you to execute actions like this:

ss.getRange("B" + currentRowNumber).getDisplayValue() // -> output: 11/20/2020

ss.getRange("F" + currentRowNumber).getDisplayValue() // -> result: 3:00 PM

Furthermore, for code readability, consider utilizing template literals:

var concatenatedDateStartTime = `${eventDate} ${startTime}`;
var concatenatedDateEndTime = `${eventDate} ${endTime}`;

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

Are you on the lookout for an Angular2 visual form editor or a robust form engine that allows you to effortlessly create forms using a GUI, generator, or centralized configuration

In our development team, we are currently diving into several Angular2< projects. While my colleagues are comfortable coding large forms directly with Typescript and HTML in our Angular 2< projects, I am not completely satisfied with this method. We ...

Display a hidden form field in Rails depending on the object's value

As a programmer learning Ruby on Rails without much knowledge of Javascript, I faced a problem with a form that creates an object called Unit. This Unit model is related to Category which in turn is related to Product. The issue was that while selecting a ...

Retrieving the path parameter in a Next.js APIabstractmethod

I need assistance with extracting information from the file routes/api/[slug]/[uid].ts Specifically, I am looking to retrieve the slug and uid within my handler function. Can anyone provide guidance on how to achieve this? ...

Encountering a problem when utilizing the each loop within an ajax request

While attempting to iterate through a each loop within an Ajax call, I encounter the following error: TypeError: invalid 'in' operand e Here is my Ajax call code snippet: $.ajax({ type: "POST", url: "/admin/counselormanagem ...

Tips for customizing the color of Menu in material-ui v5

I've been searching for solutions to change the background color of the Menu, but the methods I found are outdated. The use of @mui/styles and makeStyles is now deprecated, as stated in mui.com/styles/basics/#hook-api. I attempted to change the backgr ...

Dependency tree resolution failed during VUE installation

After pulling my project from another computer where it worked fine, I encountered an error when trying to npm install on this machine. Can someone please provide some guidance on how to resolve this issue and prevent similar problems in the future? npm ER ...

What are the drawbacks of calling async/await within a fresh Promise() constructor?

I have implemented the async.eachLimit function to manage the maximum number of operations concurrently. const { eachLimit } = require("async"); function myFunction() { return new Promise(async (resolve, reject) => { eachLimit((await getAsyncArray ...

combine and refresh identical items within an array

Currently, I am in the process of creating a prototype for an item-list and a shopping-cart. Both components function as standalone entities but are connected through a vuex-store. The item-list contains various elements that can be added to the shopping-c ...

Adjust parent div size based on image size increase

I am currently facing a situation where I have a page displaying an image, but sometimes it appears too small. In order to make the image larger, I have utilized CSS Transform and it is working well. However, the issue lies in the fact that the parent DIV ...

How to grab JSON data within a CakePHP 2.2 controller

Trying to transmit JSON data from a web page using JQuery, as shown below: $.ajax({ type: "post", url: "http://localhost/ajax/login", data: '{username: "wiiNinja", password: "isAnub"}', dataType: "json", contentType: "applica ...

Creating a worldwide object in JavaScript

I am trying to create a global object in JavaScript. Below is an example code snippet: function main() { window.example { sky: "clear", money: "green", dollars: 3000 } } However, I am unable to access the object outside th ...

Editing HTML using the retrieved jQuery html() content

I need to modify some HTML that is stored in a variable. For example: var testhtml = $('.agenda-rename').html(); console.log($('input',testhtml).attr('name')); I also tried the following: console.log($(testhtml).find(' ...

Using Javascript closures for managing asynchronous Ajax requests within for loops

Let's consider the arrays provided below. var clients = ['a','b']; var reports = ['x','y','z']; var finalData = []; Now, I aim to iterate through them in a specific manner as shown. for(var i=0;i< ...

Programmatically show/hide legend items in amCharts 4

The project includes a Line chart with a legend that are initialized in separate containers. Both components are created using a createFromConfig method. <div ref="chartdiv" /> <div ref="legenddiv" /> My goal is to store to ...

Accordion checkbox with dynamic features

Currently, I am dynamically populating data into a jQuery accordion. My goal is to include a checkbox just before the <h2> text. <div id="checkbox"> <h2> <span> <input type="checkbox" class="mycheck" value="apple" / ...

The $postLink() method in the controller is encountering a null value for this.$scope

class pageController { constructor($scope, MyEditableGrid) { this.$scope = $scope; this.MyEditableGrid = MyEditableGrid; this.myEditableGrid = { appScope: this.$scope, ..... } } $postLink ...

Populate a Textbox Automatically using a Dropdown List

MVC 4 Changing multiple display fields based on DropDownListFor selection Having some issues trying to implement the solution mentioned above. It seems like there might be a problem with either my javascript code or the controller. JavaScript in View ...

After downloading the latest version of NodeJS, why am I seeing this error when trying to create a new React app using npx?

After updating to a newer version of NodeJS, I attempted to create a new React app using the command npx create-react-app my-app. However, I encountered the following error message: Try the new cross-platform PowerShell https://aka.ms/pscore6 PS E:\A ...

Fetching Date and Time from the Internet using PHP

While I understand that similar questions have been asked numerous times before, I have yet to find a solution that fits my specific needs. My question is regarding how to retrieve the current date and time from the internet rather than relying on the loc ...

"Transforming a query into a JSON array - a step-by-step

My query generates the following output: { key:1,label:"R. Bulan"} { key:2,label:"R. Bintang"} { key:3,label:"R. Akasia"} { key:4,label:"R. Guest Room"} This is my SQL query: select '{ '||'key:'||IDMEETINGROOM||''||',l ...