I have a webapp that I get a value. I make an append row with this data for my sheet. However, I want that this data have a text format, because I want to get the value like I typed.
Some problems happen with my sheet, like an "and" be recognized as a number.
Ex: 2000e7 appears as: 2.00E + 10.
I know that yhave a function to change the format. NumberFormat (" ")
But I think that I'm not using it correctly.
My code:
var ws= ss.getSheetByName(flag);
ws.appendRow([Entrada,Modelo,SN,Ocorrencia,Status,data,obs])
var ss2 = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss2.getSheets()[0];
var range = sheet.getRange("My Last row");
// Money format
range.setNumberFormat("#");
Sometimes the better way is to prepend the value with ' rather than using setNumberFormat
Related
Script 'setValues' method interprets strings as numbers in cells
Related
I have a User Input which consists of a Date and a Time Input. Both of those values are send as a full date like
reminder_date: '2021-02-15T08:00:00.000Z',
reminder_time: '2021-02-09T17:00:00.000Z'
But i want to store it in my db as one single value like
'2021-02-15T17:00:00.000Z'
what is the best approach to achieve that ? split the strings on T and then combine or is there a simple way i can take the date part and the time part and create a new dateTime to save
If the two items are strings, you could simply use the JavaScript substr function to get the first 11 characters from reminder_date and the second part of reminder_time (starting from 11), then concatenate them, e.g.
let reminder_date = '2021-02-15T08:00:00.000Z';
let reminder_time = '2021-02-09T17:00:00.000Z';
let reminder_date_time = reminder_date.substr(0, 11) + reminder_time.substr(11);
console.log(reminder_date_time);
I am getting 2018-06-10 00:29:04 this type of value in the key date. I just want to display date without time.
I want to have 2018-06-10 from 2018-06-10 00:29:04.
If it's a fixed string that you're working with, as in you know it'll always be in that format, you can just truncate it like so:
var datetimestamp = "2018-06-10 00:29:04";
var dateTruncated = datetimestamp.slice(0, 10);
If it's not fixed, you can split on spaces like #Shubh mentioned in his comment and take the first array value.
I've been working on Google Scripts for a bit, but I can't seem to find the solution to my problem. What I am trying to do is multiply the contents of two cells on a spreadsheet together with my function calculateRates(). Here is my code:
/**
#customFunction
*/
function calculateRates(hourDecimal, personRate) {
var ss = SpreadsheetApp.getActiveSpreadsheet(); //Access spreadsheet
var sheet = ss.getSheets()[0]; //Access sheet
var range = sheet.getDataRange(); //Access all cells
var values = range.getValues(); //Call to get value of certain cell
var totalEarned = hourDecimal * personRate;
Logger.log(totalEarned);
return totalEarned;
}
The problem with it is that after I designate two cells to multiply together as the parameters of the function on the spreadsheet, there is an error that reads "Result was not a number." I know I have the function correct on my spreadsheet, so that can't be the problem..... Any ideas? Thanks for your help!
This error occurs when you try to perform calculations with values that are not all numbers, example a Number and a String.
Google Sheets stores data depending on the value of the cell, the values may be of type Number, Boolean, Date, or String (Empty cells will return an empty String). When these values are used by Custom Functions, Apps Script treats them as the appropriate data type in JavaScript.
You need to make sure the values of the cells are Numbers, take into account that if the cell contains some space or other non-numeric characters like "2 2" or "3/2" it will be treated as String and you will need to trim or extract those special characters before any calculation.
I followed this guide to export an Excel Spreadsheet as an XML data file and then this guide to display the XML sheet as an HTML table on my website. It worked great. Now I "only" have to small issues remaining that I couldn't get solved.
(1) The output table contains numbers like 1.325667 but also lots of 0s. I would like the zeroes to be displayed as 0.00 and the numbers with many decimals to be displayed as 1.33. Basically, each number should be displayed with two decimals.
(2) The excel sheet contains hyperlinks to other pages on my website that I would like to keep when rendering the XML data file and then the HTML table. So far, that didn't work. Is this possible?
UPDATE I figured this part out. By breaking up the hyperlinks in just their character-strings, then adding new columns for these character strings, and then tweaking the source code to including
document.write("<tr><td><a href='");
document.write(x[i].getElementsByTagName("character-string")0].childNodes[0].nodeValue);
document.write(".php'>");
document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
document.write("</a></td>");
I was able to include hyperlinks.
The Excel-Sheet is formatted with these two aspects already integrated, but the conversion to an XML file seems to be the problem.
Thank you so much for your help (again and again :-))
UPDATE I now also found a way to do the rounding in Excel, but I'm still stuck with integers and numbers with only one decimal. Basically, I now "only" need a way to show every number with two decimal points, applying to integers (e.g. 0 should 0.00) and numbers with one decimal (e.g. 1.5 should be 1.50). JohnnyReeves' answer seems to be on the right track but I couldn't get it to work. Any other ideas?
The Number Object has the method toFixed():
1.325667.toFixed(2) = 1.33.
Running inside the loop of the XML, select the URL and add it to the link:
document.write("< a href=" + x[i].getElementsByTagName(< your URL link>) + ">);
document.write("some text");
document.write("< /a>");
The Number.toFixed method will only work on floating point values (eg: 2.1), but not on integers (eg: 2, or 0). You will need to convert your number type to a string type so you can format it for display and get consistent results regardless of the input. A function like this should do the trick:
function formatNumber(value) {
var parts,
trailingDigits,
formattedRemainder;
// Coerce the supplied value to a String type.
value = String(value);
// Break the supplied number into two parts, before and after the dot
parts = value.split(".");
// If there was no dot, there will only be one "part" and we can just
// add the trailing zeros.
if (parts.length === 1) {
formattedRemainder = "00";
}
else {
trailingDigits = parts[1];
if (trailingDigits.length === 0) {
// A dot, but no digits. (eg: 2. -> 2.00)
formattedRemainder = "00";
}
else if (trailingDigits.length === 1) {
// Add an extra trailing zero (eg: 2.1 -> 2.10)
formattedRemainder = trailingDigits + "0";
}
else {
// Just take the last two trailing digits.
formattedRemainder = trailingDigits.substring(0, 2);
}
}
// Build the final formatted string for display.
return parts[0] + "." + formattedRemainder;
}
Here's the breakdown:
What code do I need to extract the 12, 26, and 48 from a field whose value is 101219488926 and then display it in the format MM/DD/YY? (So in this case, the new value would need to be 12/26/48)
Here's the long version:
I'm using a mag-stripe reader that takes information from the swiped card (a driver license) and then uses that info to auto-populate certain fields in a PDF (first name, last name, date of birth, etc).
Everything works fine, with one exception: the date of birth. Even that does technically work, but the value is in this format (assuming the person's DOB is 26 December 1948):
101219488926
What I need is: the month (12), day (26), and year (1948) stripped out of that long number, then converted to display in the format MM/DD/YY
Outside of Acrobat, this seems to work just fine:
var dob = 101219488926;
trimmonth = dob.substring(2,4);
trimday = dob.substring(10,12);
trimyear = dob.substring(6,8);
dob.value = trimmonth + "/" + trimday + "/" + trimyear;
Any suggestions?
The code you have there shouldn't work - substring is a String function, so you would need to convert that number you have to a string for it to be available. Setting dob.value is also suspect, since dob is a Number, and numbers do not have a value property.
Of course, it's obvious that you're not showing the actual code you have tried, but something like this would probably work:
// Appending blank string to type coerce
var dob = 101219488926 + '';
// Array.join to glue the numbers together
// (no reason why you **have** to use this; the original method will work fine too)
dopInput.value = [dob.substring(2,4), dob.substring(10,12), dob.substring(6,8)].join('/');