I have a Google Sheets Apps Script that uses this method to store data:
sheet.getRange(rowNumber, colNumber).setValue(theString);
Later, those values are retrieved using:
theRowData = sheet.getRange(r, FIRST_DATA_COLUMN, 1, MAX_COLS).getValues()[0];
This seems to be working fairly well except when theString contains a value that looks like a date (such as "6-Dec-2020". Then, getValues() seems to be retrieving a date value (and then converting it to a very verbose string) rather than the string that was written. In this case, I do not want such evaluation of what the data means. I intend to write these values as constants that don't need nor desire interpretation (such as would be done by apostrophe in Excel with a value like "'6-Dec-2020"). Is there a way to retrieve exactly the same value that was stored (or the store exactly the value that was intended), in exactly the same format?
For this specific problem example, I'm getting back "Sun Dec 06 2020 00:00:00 GMT-0500 (Eastern Standard Time)" when what I wrote (and what I want returned) is "6-Dec-2020" . I do realize I could set number formats, but that won't always work, as these values (that admittedly look like dates) are sometimes in other formats, like "6-Dec" among others, and I would need "6-Dec" back when reading in that case.
Use getDisplayValue() or getDisplayValues().
The displayed value takes into account date, time and currency formatting formatting, including formats applied automatically by the spreadsheet's locale setting
Related
If we have only a time in say cell A1 of Google sheets. For example, I have this:
8:09:00 AM
When I use this to fetch the contents of the cell: sheet.getRange(1, 1).getValue();
this is what it returns:
Sat Dec 30 1899 08:09:00 GMT-0500 (EST)
Why is this happening? Any way for getvalue() to stop interpreting the data and get the raw data instead?
Note: No special formatting has been set by me for the cell. So I guess the formating is the default (automatic), which is not something I want to change for every cell containing such data. The cells containing such times will not be known apriori in any case.
Use range.getDisplayValue() to get the display value as a string.
More details on this can be found here: Difference between getValue() and getDisplayValue() on google app script
In short, getvalues returns an object which can be a number,a date or a string. In this case it returns a date object, which why it get formatted differently.
I am importing excel data using html, spring and apache POI API. I need to do some validations before saving the data.
For a cell, I need to check whether its value is greater than 01 and less than 32.
I have seen like
cell.getStringCellValue().matches(regex);
Using this I have reached in a solution like this
if(cell.getStringCellValue().matches("\b([1-9]|[12][0-9]|3[01])\b"))
But I have read like, using regExp for comparing numbers is not such a good Idea, it is mainly for strings.
So my question is , is there a better way to solve my problem, finding whether the cell value falls within a range.
You need to add 0 before [1-9] inorder to match the number range from 01 to 09.
\b(0[1-9]|[12][0-9]|3[01])\b
I'm trying to automate the creation of a monthly report, which have some financial information like "R$ 5.000,00". However, AutoCrat insert this information as "5000", losing all the number format.
Does anyone know a way around this?
If you have questions or feature requests that are specific to the Autocrat Script, visit the author's page.
When a script reads a numeric value from a spreadsheet, it gets only the value. It is possible to also retrieve the format as a separate operation, but it appears that Autocrat is not doing that.
As a work-around, you could format that information as a string, rather than a number. The TEXT() function will do this for you:
=TEXT(--cell--,"R$ #,##0.00")
The screenshot below shows that the formatted text in B2 looks the same as the numeric value in A2, except for the horizontal alignment. If we then had Autocrat use the values in column B, the formatting would be preserved.
I am having a few issues using SSRS-Reports 2005.
The first one is I am trying to use the datediff function to change the background color of a cell based on the two dates being within 30 days of each other.
=iif(
DateDiff("d",DateString,Fields!Insurance_Certificate.Value)<= 30, "Tan", "White"
)
I have my fields formatted through the initial query so they look like mm/dd/yyyy. I guess my first question is how do I see what value is being evaluated because whatever this is returning can't be right.
my [...] question is how do I see what value is being evaluated
There is no real "debugger" available like you would have in -say- a WinForms C# app. Instead, you have several "raw" "debugging" options:
Render Fields!Insureance_Certificate.Value in a seperate cell, as text
Render DateDiff("d",DateString,Fields!Insurance_Certificate.Value) in a seperate cell, as text
Right-click your dataset, select "Query...", and execute the query manually. Inspect the values for your field. Make sure they're what you'd expect.
Render your DateString in a seperate cell, with and without a cast to a date.
Other than that #MarkBannister has a great suggestion, using actual Dates as opposed to strings for your fields and variables. One additional thing to note about this, is that date parsing may be culture-specific. Be sure you understand and know in what culture your DateString is being parsed. The above "debugging" options may help you find out.
I suggest querying your date fields as dates (instead of as strings), comparing them using the DateDiff function as in the question and formatting the date output using the Format property of the appropriate textboxes in SSRS.
I have a google spreadsheet in which I want a date with only the name of the month and the year, like September 2011, and I also want the month and year to be easily changeable.
Is there any way of getting custom date formats to do this?
I figured out I could do like this:
=TEXT(40295; "MMMM yy")
But then the datepicker can't be used anymore and changing the date is made impossibly hard..
Is there any good way of solving this?
You can set a custom format to a cell using Google Apps Script.
Open the script editor (menu Tools > Script editor), paste this, save and Run > onOpen.
function onOpen() {
SpreadsheetApp.getActive().addMenu(
'Format', [{name:'Custom', functionName:'customFormat'}]);
}
function customFormat() {
var format = Browser.inputBox('Write the format to be applied on the seleted cells');
if( format !== 'cancel' )
SpreadsheetApp.getActiveRange().setNumberFormat(format);
}
On your spreadsheet a new menu should appear in the end where you can pick the Custom entry to enter your custom format for the selected cells.
Google Spreadsheet does not yet permit you to apply a custom number format to a cell.
You can of course enter the date into a cell, and then reference that date in a second cell:
A1:4/27/2010, A2=TEXT(A1;"MMMM yy")
This would meet your requirements: it would display the date the way you wanted, and allow the date to be easily changeable.
But it has the undesirable side effect of having the date appearing twice on the sheet. I often work around side effects like this by printing or exporting a range instead of the entire sheet. So maybe there is also a practical workaround in your case.
I thought yy just gives the 2 digit year.
I used the following:
=text(E2,"MMMM YYYY")
E2 was the specific cell I used, but you could use any cell.
You can enter any format (for dates or others) as a Custom Number Format.
Highlight the cell range and Go to Format > Number > More Formats > Custom Number Format. Then enter
mmmm" "yyyy
gives "September 2011"
or any other format
ddd" "mm"/"dd"/"yyyy
would give "Mon 09/11/2011"
note the missing quote at the beginning and the end.
it shows how it will display as you experiment.
Quotes in the beginning or end give you invalid format
Saves you having two fields (the data, and the text() formatted one)
Its not intuitive (either the format, or where to put it). But works better than importing an xls.
I accidentally found a workaround for a custom date format. I had a custom date format using Excel. When uploading the Excel file, the date format (mm/dd/yyyy hh:mm am/pm) stayed in that format even though that was not a supported Google Sheet format. Then using the format painter, I was able to copy that format to other cells within Google Sheet. I know this is not an ideal solution, but seems to work. I have not played with how many other custom formats I could create in Excel, convert to Google Sheet and then use format painter to use with other cells.