Google Script Sheets API - default number format - google-sheets-api

It looks like google sheets is making the same mistake as Excel, by "thinking ahead" and converting the value "1.1.1" to 2001.01.01 when doing sheet.appendRow. I have tried to set the number format of the column in charge to "#" (which should be plain text) before inserting rows - but looks ineffective. On the other hand doing the same after inserts is also ineffective, as the content is already "date".
Adding ' before is working, but it is not what I need.
Is there any way to give a default format or to disable such automatic conversion (from google script)?

I have a cell in which a form deposits a variable number of values seperated by a comma such as: " 1, 2, 4, 6 " etc - when there are only three answers, Google "helps" me by converting the value into a date object. But it's supposed to be a list of choices...
It's not pretty, but I've managed a workaround by using .getDisplayValue instead of .getValue - it does change the cell value into a string, so if you need to do further manipulations that are dependent on the value being a number or something, obviously, this fails.
I overwrite the value for the problem cell in my array before passing it to .appendRow
//getting the values
var values = s.getRange(row,1,1,lastCol).getValues()[0];
//brute force crushing of problem value
values[5] = s.getRange(row,6).getDisplayValue();

Related

vba customize cells format

I am needing to customize cells with simple thousands format, like 1000, without any separator or decimal.
However, I wish to remove text fonts other than a number when they are input.
For example, I want to input 120118, however in my paper from which I am copying that figures, it is formatted as a date, thereby 12/01/18. I am needing Excel to simply keep it as 120118 after typing, removing the slash (/). I have seen similar settings in access queries.
Have you tried simply pasting only the cell value with:
Selection.PasteSpecial Paste:=xlPasteValues
Or just clear the cell format and format it again with your desired format.
Try:
Selecting the range
Home > Number > Number Format (or Ctrl+1 I think) > Custom
Enter ddmmyy
Okay
Can be done programmatically e.g.
Thisworkbook.worksheets("Sheet1").range("A1:A50").numberformat = "ddmmyy"
The above would only be a visual/cosmetic change and the internal value of each cell would still be a date (technically a number) for calculation purposes.
However, if I've misunderstood and you instead want to go from the date 21 Jan 2018 to the number 210118, I think you would need to get the range's value(s), format as DDMMYY string, then clng() - or maybe (DD*10000) + (MM*100) + (YY) might work, then format as "000000" to preserve leading zeros.

Changing the title of a column in a PivotTable

I am having trouble changing the title of some columns in a pivot table. I'm trying to make them have dates in them. Each date 6 days further from the last.
Like this
But, I cannot get an equation inside the column title to stay, every time I type in the equation and press enter, it evaluates to either 0 (If the format of the cell is number or general), or 1/0/1990 (If formatted as a date). I checked the value of the cell by =ISTEXT(A1) and it evaluates as true. No matter how I format the cell. So I can never change the title to look like the picture. Any ides?
Here is what I have.
TRUE is the result from ISTEXT()
Even if I manually enter in the formula via the function arguments, it'll show up correct, but when I click ok. It will go back to either 0 or 1/0/1990
Here's the original page
https://drive.google.com/file/d/0B3p8Jm7oNAo4ZUN0Qk1mR1cxYmM/view?usp=sharing
In Excel, dynamic values (formulas) in the header of a table-formatted table are not allowed.
Instead, you can first generate your table header and then format the table as (pivot-)table. You should get a message saying that the header row will be converted in static text (with correct format).

Why don't numeric cells have a .Characters() property?

With any text value, I can individually format each character and then copy that formatting to another cell by iterating over the Range.Characters() Collection.
However, if the cell is a number (even if the numberFormatting displays it as a string e.g. dates) then it does not expose a .Characters() property and, indeed, cannot be selectively formatted digit-by-digit.
Why does Excel display strings using Character objects but not numbers, even when the number is being displayed as a string?
If you want to go around this, you may do the following:
In cell A1 put '123456 with the " ' " sign in front.
Then write
range("A1").Characters(1,3).Font.Bold = true
It would take only the first three numbers, not taking into account the " ' " sign. Thus, the number is kind of displayed as a string, but you can still use it calculations e.g. A1 + 4 would give 123460.

Excel VBA InputBox value is not a date

I have a user input box where you type in a string, annoyingly this string looks like a date 00/00/0000 and excel reformats it as such.
When the value can't be a date ex. 18/19/4561 (month can't be 18 or 19) it displays it correctly.
But whenever it can be seen as a possible date it switches things around.
I've tried setting the value as a string rather than nothing but excel still changes it when putting it in the page.
When I try manually inputting it in the cell or equal the values from a manually entered cell it works fine.
But whenever I get it from the inputbox it messes with it. Even when I hard code the string to a variable (x = "05/06/4564") it switches things around.
How do I force excel to leave the string as is?
Prefix the value with a single apostrophe and Excel will interpret it as a string.
Eg '18/19/4561
Also, have you tried setting the cell format to Text

How to use UserForm Values for multiple things

I have UserForm1 which is a multipage userform and I am trying to access the information that was gathered through the form in a sub located in Module1. This sub will need to access several different values and do different things with those values so this is going to be a multipart question.
I have the below code in which I attempt to use one of the values as the upper limit of a For Next Loop. However the current problem is that when the code reaches this line it jumps to the Userform_Initialize routine.
For X = 1 To UserForm1.LocalOffer.Value
Second part of this question comes from inside the For Next loop from above. Where I have the below code. Which would ideally allow me to cycle through a series of similarly named Textboxes from the userform. Not even sure if that will work as the code keeps breaking before getting to that part.
Range("B" & X).Value = UserForm1.Controls("LocalTier" & Tier).Value
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
I am using Excel 2013 but the file will be ran on Excel 2007
Edit:
Turns out that problem 1 was fixed by not closing the userform with the X button but instead adding a line to hide the userform when you hit the last button. As it turns out my code for the second question worked just fine once i got past that. Only question left is the last one which I have no ideas on.
As from the comments, I see you don't need anymore to know about points 1 and 2, I will hence limit my answer to the point 3.
Last Part of this question if I have a Textbox in the userform that contains a date in the format 1/18/2015 is there a way for me to grab just a portion of that date say for instance just the Day or just the last digit of the year?
You can use either string manipulation, or date conversion.
Let's assume the Textbox is called myDateTextbox
String manipulation
Among the string manipulators that VBA provides, I would cite Left() and Right().
For example:
last_digit_of_the_year = Right(myDateTextbox.Text, 1)
will return you the last character of the string. On the other hand:
first_digit = Left(myDateTextBox.Text,1)
will return you the first digit of the string.
You can use the Len(myDateTextBox.Text) built-in to return the current length of the string.
Date conversion
You can simply convert your string into date using the CDate() function. Please note this function will return an error if you pass an invalid string. If your textbox contains 24/01/1990, you can first convert the string into a date:
myDate = CDate(myDateTextBox.Text)
Hence, you can retrieve day, month or year like this:
myYear = Year(myDate)
myMonth = Month(myDate)
myDay = Day(myDate)
Please note that CDate recognizes date formats according to the locale setting of your system.. Hence, if the format in the TextBox is not the same than the one of your system, then consider manipulating the string before to adapt it to the proper format. For example, if your system has settings DD/MM/YYYY and your textbox shows a MM/DD/YYYY type, you can "adjust it" as follows:
wrongFormat = myDateTextBox.Text
splittedDate = Split(wrongFormat,"/")
goodFormat = splittedDate(1) & "/" & splittedDate(0) & "/" splittedDate(2)
If wrongFormat was 1/18/2014 but your system would like to read it as 18/1/2014, it's now fine because goodFormat will be equal to 18/1/2014 after the split and re-build.