Format cell in google sheet to number WITHOUT changing it - google-sheets

I have a kinda big google sheet. Around 20k rows. One of the cells in these rows contains always a 17 digit number. I automatically adding new rows via the google API with the nodeJS package (https://www.npmjs.com/package/google-spreadsheet).
Here is my problem, everything works fine if the last two digits in the number arent two zeros. If its 12345678912345678 then the "automatically" formating works fine. It shows it as it is and I can read it with the NodeJS package as it is.
But if its 12345678912345600 then its gets automatically converted to 1,23456E+16.
Now I want to convert it back to "normal". Thought kinda easy, just use format "text" and it should be displayed correctly. I dont care if its a number or a text, I can handle that on my side of the script. But formating it to text change the value of the cell to 1,23456E+16. Not what I want.
Okay, then convert it to a number, no problem. The normal default number formats doesnt fit (Since I dont want any . or , or decimal or anything, just the plain number. Custom number format it is. So I select custom number format and even the first example is what I want. So I select the format 0. Its just display the number without anything. Works exactly as I want.
Okay, lets do it for all of the 20k rows. I have roughly 1k rows with a double zero at the end. So I select the complete column, custom number format 0 and done?
Nope... Next problem. If you convert a number with more than 15 digits, every digit after the 15th digit get converted to 0. So I change all my other rows.
I could go and select all the affected one thousand rows by hand and just convert these rows, but yeah... one thousand rows...
Isnt there any way to convert these numbers just to display the number? Without changing it?

Try updating the format NUMBER to # to display the plain number:
A sample script:
const changeFormatNumber = async () => {
const auth = await getOAuthClient()
const sheet = google.sheets({ version: 'v4', auth })
const prop = await sheet.spreadsheets.batchUpdate({
spreadsheetId: '<SPREADSHEET_ID>',
requestBody: {
requests: [
{
repeatCell: {
fields: 'userEnteredFormat.numberFormat',
range: {
startRowIndex: 0,
startColumnIndex: 0,
endColumnIndex: 1,
endRowIndex: 100
},
cell: {
"userEnteredFormat": {
"numberFormat": {
"type": "NUMBER",
"pattern": "#"
}
}
}
},
}
]
}
})
console.log(prop)
}
Before:
After:
Documentation
Basic Formatting
Cell Data

Related

How to increase google sheets/excel cell count by one and then reorder rows

I am not familiar with google sheets or excel, so I'm not even sure if what I want to do is possible.
I have one thousand rows in a google sheets document. I need a column that has a number to represent the row. Starting at an arbitrary row (lets say row 5 in the document), That cell value needs to be 1. The next row needs that column value to be 2. Then 3, 4, 5, 6... all the way to 1000. It's not feasible to do that manually, so is there a way to automatically fill in the cells with the values I need?
This next part is what I can't figure out. I've found a few solutions to the first part, but none that work with this extra condition. After I generate these numbers, I need to reorder the rows (reordering can't be done before). The problem is that if I use some formula in google sheets, as soon as I generate the numbers then reorder the rows, the formula either breaks or recalculates the numbers, setting them back to 1, 2, 3..., when (for example) after the reorder I would expect 42, 815, 934...
Is what I want to do possible, and if so how can I accomplish this?
Besides the solution that has already been provided, you can also make use of Apps Script and write your own script in order to change the cell values.
Some methods which can be of help to you are:
getRange() - in order to retrieve the range from the sheet;
setValue() - in order to set the value of the cell/s;
Thererefore, your script will end up something similar to:
function setCellValues() {
let spreadsheet = SpreadsheetApp.openById("SS_ID");
let sheet = spreadsheet.getSheetByName("SHEET_NAME");
let startRow = "START_ROW";
let startCol = "START_COL";
for (let i = 0; i < 1000; i++) {
sheet.getRange("START_ROW" + i, "START_COL").setValue(i + 1);
}
}
As for reordering the rows, you can use Apps Script for this again or a simple SORT function.
Reference
Apps Script Sheet Class - getRange();
Apps Script Range Class - setValue();
SORT function.

Remove trailing ".00" from numbers in Google Sheets

In Google Sheets I'm trying to format numbers to have format "0.##" - so that integer be like integer (12 be exactly 12), decimals be like decimals with no trailing zeroes (12.30 be 12.3 and 12.4321 be 12.43), but in reality
for 12 I've got 12. with this annoying decimal point and see NO WAY to get rid of it.
The same time if I choose Format -> Number -> Automatic, I've got calculated numbers be like 131.3666667 which is not my desired format.
In LibreOffice using format "0.##" removes unnecessary decimal point, but Google Sheets don't. If you know how to do it, please share your knowledge. Googling doesn't help much.
Thank you in advance!
Number format 0.## works well with Google Apps Script. It does not leave dot to numbers that has no decimals.
The code below will apply number formatting to the whole Sheet.
Try this:
function formatColumn() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Sheet1");
var range = sheet.getRange(1, 1, sheet.getMaxRows(), sheet.getMaxColumns());
range.setNumberFormat("0.##");
}
*Note:
You can change the range to column specific by changing the value of getRange(). Example: sheet.getRange(A1:A).
If you choose to format the whole sheet, you only need to run the script once to apply the format.
Output:
Before:
After:
Since we set the format to the whole sheet, It will automatically format any inputted numbers.
Reference:
Google Apps Script
Class Range
Range.setNumberFormat(numberFormat)
hi i found the Solution :
For Example let us say that you have the number 100 in the cell A1 ,
and you have the Number 99.9 in the cell B1 .
let us say you want to get the sum of them without having the.9 .
click on the cell that you want to add the sum on it :
let us say it is the cell C1 click on it then add =INT //(MUST BE CAPITAL) :
for example :
= INT(A1 + B1)
then hit Enter .
now the value of the sum is :
199
If you don't use the result of these cells for further calculation (or if you do use, but don't mind losing some precision), you can surround your current formula with Round(..., 2) and leave the format as automatic.
Some examples of what original numbers will appear as:
1.004 -> 1
1.006 -> 1.01
1.096 -> 1.1

How to SPLIT cell content into sets of 50000 characters in new columns Google Sheets

I have 3 columns A, B & C as shown in the image. Column A contains the search key. The second column B contains names and their respective content in the third column C.
I am filtering rows that contain the text in A1 in B:C and concatenating them. The challenge is that each text in the third column is roughly 40k characters. The filter formula works well so the issue is the character limit. This formula =ArrayFormula(query(C1:C,,100000)) which I have in F1 concatenates more than 50000 characters but I am not how to apply it for my case.
Tried to wrap my formula in E1 inside the query function but it wasn't successful. Like so:
=ArrayFormula(query(CLEAN(CONCATENATE(FILTER(C1:C, B1:B=A1))),,100000)).
I also tried to SPLIT the concatenated result into sets of 50000 characters and put the extras in the next columns but wouldn't manage either. The formula I tried in this case is:
=SPLIT(REGEXREPLACE(CLEAN(CONCATENATE(FILTER(C1:C, B1:B=A1))),".{50000}", "$0,"),",")
The link to the spreadsheet
https://docs.google.com/spreadsheets/d/1rhVSQJBGaPQu6y2WbqkO2_UqzyfCc3_76t4AK3PdF7M/edit?usp=sharing
Since cell is limited to 50,000 characters, using CONCATENATE is not possible. Alternative solution is to use Google Apps Script's custom function. The good thing about Apps Script is it can handle millions of string characters.
To create custom function:
Create or open a spreadsheet in Google Sheets.
Select the menu item Tools > Script editor.
Delete any code in the script editor and copy and paste the code below.
At the top, click Save.
To use custom function:
Click the cell where you want to use the function.
Type an equals sign (=) followed by the function name and any input value — for example, =myFunction(A1) — and press Enter.
The cell will momentarily display Loading..., then return the result.
Code:
function myFunction(text) {
var arr = text.flat();
var newStr = arr.join(' ');
var slicedStr = stringChop(newStr, 50000);
return [slicedStr];
}
function stringChop(str, size){
if (str == null) return [];
str = String(str);
size = ~~size;
return size > 0 ? str.match(new RegExp('.{1,' + size + '}', 'g')) : [str];
}
Example:
Based on your sample spreadsheet, there are 4 rows that matches the criteria of the filter and each cell contains 38,976 characters, which is 155,904 characters in total. Dividing it by 50,000 is 3.12. The ceiling of 3.12 is 4 which means we have 4 columns of data.
Usage:
Paste this in cell E1:
=myFunction(FILTER(C1:C, B1:B=A1))
Output:
Reference:
Custom Function

Cannot append uniformly using Google Sheet Api V4 with empty values

I am trying to figure what combination of options for the (Google Sheet Append Api) I need to uniformly append a row of data. My data could include empty values or not.
Problem:
I cannot seem to figure out a way to guarantee my row to append to a sheet starting from A1 every single time.
Here are the different behaviours I have seen:
1) { values: [["", "", 3]] }
If I append this row 3 times, the rows will cover the ranges A1:C1, C2:E2, E3: G3. What I really want is A1:C3.
2) { values: [["3", "", 3]] }
If I append this row 3 times, the rows will do something similar to 1) where it will cover the ranges A1:C1, C2:E2, E3: G3. Again, I want A1:C3.
3) { values: [["3", false, 3]] }
If I append this row 3 times, everything works the way I want it to. It covers the ranges A1:C3.
What options do I specify in my append api request to guarantee that my rows will always start at A1 (even if the first n columns are empty)?

COUNTA not working as I expect in new Google Sheets

I'm using the command =COUNTA(I31:I) to count all non-empty cells in the column. The cells in the column contain this line of code:
=IF(ISNUMBER(D31), "X", "")
The result is that the cells are either an empty string (the line with add nothing "") or contain an X.
The problem now is that COUNTA counts all the cells after this procedure, empty or not. If I delete the long command line the cell is not counted and it works fine. So this long command line is adding something to the cell, so it appears not-empty even though it looks empty.
Any clues to what that is? My guess is that I should use some kind of Null operator instead of the "" to add nothing to the cell.
Note: For some reason, this seems to work in a previous version of Google Spreadsheet I had.
As a potential workaround, I could replace =COUNTA(I31:I) by checking specifically for x with =COUNTIF(I31:I;"X"). But I'm still curious as to the problem with COUNTA.
If it turns out that my cells are not empty 'enough' for this command, how can I then make them completely empty?
See Also: Count rows with not empty value
Unfortunately, this is functions as designed from Google.
Although I'm not sure why it's divergent from the way that Excel calculates COUNTA.
According to the Google Spreadsheet Documentation on COUNTA:
COUNTA counts all values in a dataset, including those which appear more than once and text values (including zero-length strings and whitespace).
Meaning that the only way to "make [the cells] completely empty" is to delete the entire contents, formula and all. But fear not...
Some workarounds:
Hypothetically, you should be able to do this with =COUNTIF(A3:A8,"<>"&""), but Google spreadsheets doesn't support the not equal to operator in the COUNTIF function according to these forums: 1, 2, 3
A workaround is to create a truthy or falsy array based on the condition and use SUMPRODUCT to count the truthy values like this:
=SUMPRODUCT((A3:A8<>"")*1)
Another option you could pursue would be to write a custom function and add it to Drive.
It's actually pretty easy to do so. Go to Tools > Script Editor and add the following script:
/**
* Works like COUNTA except does not include null strings
*
* #param {cellRange} myArray The value or range to consider when counting.
* #return Returns the a count of the number of values in a dataset.
* #customfunction
*/
function CountNotBlank(myArray) {
var output = 0;
for (i = 0; i < myArray.length; i++) {
if (myArray[i] != "") {
output += 1
}
}
return output;
}
Then use like this:
=CountNotBlank(I31:I)
I would try: =IF(ISNUMBER(D31), "X", iferror(1/0))
I am told that the iferror(1/0) returns nothing at all.

Resources