I have a code that takes each cell from a gsheet and changes the format using appscript but this does not correct it back on the google sheet. I used this because even though the format was correct on the gsheet, when getvalue() is used, the number loses its format.
var Qty1 = ss.getRange(i, 15).getValue();
var Qty1Format = Qty1.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
//Output has a thousands separator and two decimal places.
This takes a lot of time to run and as a result I am looking for alternative ways to correct the format.
I was thinking of getting all the values of the column as an array and I am looking to convert the array in the format needed and paste this back into the sheet.
I've had attempts at coding this but would be grateful for any help on how to change format for the array or alternative ways of achieving the outcome.
Sample code attempt:
function copypastetest() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var rng = ss.getRange("C2:"+"c"+lr).getValues();
var frng = rng.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, '$1,');
var copy = ss.getRange("C2:"+"c"+lr).setValues(frng)
}
Further Edit:
#Cooper's answer is spot on. However I'm might not have asked the right question to solve my problem. I am ultimately looking to take values from the google sheet and replace placeholders into a google doc.
See below (although the number is formatted it still appears to be unformatted in the formula bar - and I should have noticed this before but i did not)
So how I can format the array (or get an array that is formatted in the first place to come on my Logger.log on the appscript?
Here is the rest of the script for you to understand what I am looking to achieve,
function generatetest() {
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1").activate();
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var lr = ss.getLastRow();
var rng = ss.getRange("A1:"+"F"+lr).getValues();
for (var i =2;i<=lr;i++){
if(ss.getRange(i, 1).getValue()){
var client = rng[i-1][1];
var email = rng[i-1][2];
var documentId = DriveApp.getFileById('1j36HPQkTPc0R4GCtA0XKcmeHUVPsgBKoyNIl93HFhp0').makeCopy().getId();
DriveApp.getFileById(documentId).setName(client);
var body = DocumentApp.openById(documentId).getBody();
body.replaceText('{Name}', client).replaceText('{Email}', email)
}
else {}
}
}
If I understood your issue correctly, I believe it can be solved by using the getDisplayValues() method of class Range. This method copies the format of the cell and returns the formatted string.
var rng = ss.getRange("C2:"+"c"+lr).getDisplayValues()
References:
Range.getDisplayValues()
I tried this and it seems to work, if I understand your issue.
function runOne() {
const ss=SpreadsheetApp.getActive();
const sh=ss.getSheetByName('Sheet22');
const rg=sh.getRange(1,1,sh.getLastRow());
const vA=rg.getValues();
vA.forEach(function(r,i){
sh.getRange(i+1,2).setValue(r[0]).setNumberFormat('#,##0.00');
})
}
Here's my start data:
1000000.33
2000000.34
3000000.35
4000000.36
5000000.37
6000000.38
7000000.39
8000000.4
9000000.41
10000000.42
11000000.43
12000000.44
13000000.45
14000000.46
15000000.47
16000000.48
17000000.49
18000000.5
And here's my ending data:
1000000.33,1000000.33
2000000.34,2000000.34
3000000.35,3000000.35
4000000.36,4000000.36
5000000.37,5000000.37
6000000.38,6000000.38
7000000.39,7000000.39
8000000.4,8000000.4
9000000.41,9000000.41
10000000.42,10000000.42
11000000.43,11000000.43
12000000.44,12000000.44
13000000.45,13000000.45
14000000.46,14000000.46
15000000.47,15000000.47
16000000.48,16000000.48
17000000.49,17000000.49
18000000.5,18000000.5
Here's what the sheet looks like:
I use this script a lot for viewing and editing number formats on a spreadsheet and I find it helpful to solve formatting issues. There's probably an easier way but I haven't found it yet.
function getandSetActiveRangeFormats() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var rg=sh.getActiveRange();
var fA=rg.getNumberFormats();
var html='<style>th,td{border:1px solid black;}</style><table><tr><th>Item</th><th>A1 Notation</th><th>Number Format</th><th>Enter Format</th><th>Set Format</th></tr>';
var item=1;
var row=rg.getRow();
var col=rg.getColumn();
fA.forEach(function(r,i){
r.forEach(function(c,j){
var txt=Utilities.formatString('<input type="text" id="RC-%s-%s" />',row+i,col+j);
var btn=Utilities.formatString('<input type="button" value="Set Form" onClick="setFormat(%s,%s);" />',row+i,col+j);
html+=Utilities.formatString('<tr><td>%s</td><td>%s</td><td>%s</td><td>%s</td><td>%s</td></tr>',item++,sh.getRange(row + i,col + j).getA1Notation(),fA[i][j],txt,btn);
});
});
html+='</table><input type="button" value="Exit" onClick="google.script.host.close();" />';
html+='<script>function setFormat(row,col){var f=document.getElementById("RC-"+row+"-"+col).value;google.script.run.setFormat(row,col,f);}</script>';
var ui=HtmlService.createHtmlOutput(Utilities.formatString(html));
SpreadsheetApp.getUi().showModelessDialog(ui, "Display and Set Active Range Formats")
}
function setFormat(row,col,format) {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
sh.getRange(row,col).setNumberFormat(format);
}
Related
I want to use google script to make a request and save some data on google sheet.
The problem is when I try to pass some non integer values as parameters in the request.
I think it's something to do with the fact that sheets uses comma for separating decimal from integer while my program sends the numbers separated by a dot.
This is where I am now:
const doPost = (event) => {
console.log(`doPost`, JSON.stringify(event));
const { parameter } = event;
const { temp, peso } = parameter;
var date = new Date();
var sheet = SpreadsheetApp.getActiveSheet();
sheet.appendRow([date, parseFloat(temp), peso]);
}
When I make a post request with parameters: { temp:1.234, peso:1.234 } the result on google sheet is a big mess.
Does someone have any idea how to fix this?
edit:
function Test(){
var sheet = SpreadsheetApp.getActiveSheet();
var d = 1.23456;
var date = new Date();
sheet.appendRow([date, d]);
}
This works fine... don't know if it can help you debug.
Post Data to Sheet
I don't actually do this all that much so I am by no means an expert at it
function doPost(e) {
Logger.log(e.postData.contents);
const ss = SpreadsheetApp.getActive();
const sh = ss.getSheetByName("Sheet1");
let data = JSON.parse(e.postData.contents);
sh.appendRow([data.first,data.second])
}
function sendData(obj) {
const url = ScriptApp.getService().getUrl();
const params={"payload":JSON.stringify(obj),"muteHttpExceptions":true,"method":"post","headers": {"Authorization": "Bearer " + ScriptApp.getOAuthToken()}};
UrlFetchApp.fetch(url,params);
}
function saveMyData() {
sendData({first:"1.234",second:"1.432"});
}
This worked for me.
Finally figured it out!
The problem was in the setting of the google sheet file.
I'm from Italy so it enters as defult the Italian format. For some reasons this mess up all the new entries. The solution is very simple: change the format to the english one and the problem should solve itself!
In Google Sheets Script, how do I stop limit a number to e.g. 3 decimal points?
For instance, the following code is a section of a function I have created which will send an email of the number in cell 'Q12', however the number I receive is e.g. 1.9468186134852794% instead of 1.94%.
var changeCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheetname").getRange("Q12");
var change = changeCell.getValue();
var changeFixed = change.toFixed(3);
Thanks
function fixingDecimalPlaces() {
var ss=SpreadsheetApp.getActive();
var sh=ss.getActiveSheet();
var v=73.123456789;
sh.getRange('A1').setValue(Number(v).toFixed(3));
}
If you want to send the value in Q12 as it is shown in the sheet (with the same number of decimals), you can use getDisplayValue:
var changeCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheetname").getRange("Q12");
var change = changeCell.getDisplayValue();
This won't change the value in the spreadsheet. If you want to do that, just use setValue:
var changeCell = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("sheetname").getRange("Q12");
changeFixed = changeCell.getValue().toFixed(3);
changeCell.setValue(changeFixed);
Reference:
Range.getDisplayValue()
Range.setValue(value)
I'm trying to use HTML and JS to write into a Spreadsheet and I found some useful code to do that. I had to add a column to insert an ID for each entry and I did it like this:
var sheet = SpreadsheetApp.openById("myKey").getSheetByName('Richieste');
var column = 1;
var colArray = sheet.getRange(2, column, sheet.getLastRow()).getValues();
var maxi = Math.max.apply(Math, colArray);
var id = maxi+1;
// set other params
var vals = [id, date, name, surname, serial, eMail, area, text, ans, flag];
var sheetObj = sheet.appendRow(vals);
Now, if I have an empty sheet, it works for the first two entries: the third ID is set such as 02/01/1900 0.00.00 and you can see a screenshot here.
I cannot understand what's going on... do you know something about this?
Thanks a lot for your help!
S.
It is hard to tell from the information you provided, but it looks like your cell format is set to date time, not a number.
I am trying to write a script in google sheets that will send one of two different emails based on the response to a multiple choice question. I can get my if/else statement to send either one or the other of the emails but it will not recognize the text of the multiple choice answer and send the correct email.
Here is the full script:
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 2; // First row of data to process
var numRows = 1;
// Fetch the range of cells A2:B3
var dataRange = sheet.getRange(startRow, 1, numRows, 8)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var title = row[1]; // First column
var startDate = row[3]; // Second column
var endDate = row[4];
var description = row[2];
var location = row[6];
var eventImport = row[5];
var emailAddress = row[7];
var multchoice = row[8];
if (multchoice == "Part Time") {
var subject = "New Hire Part Time Email - " + startDate;
var emailBody = "Congradulations"
var htmlBody = "Congradulations! Part time person"
MailApp.sendEmail (emailAddress, subject, emailBody);
} else {
var subject = "New Hire Appointment - " + startDate;
var emailBody = "Congratulations! We are excited"
var htmlBody = "Congratulation! </i><br/> <br/> We are excited"
MailApp.sendEmail(emailAddress, subject, emailBody);
}
}
}
I believe the problem is here:
if (multchoice == "Part Time")
Any help is greatly appreciated! I am a novice
It looks like you are assigning your variables starting with '1' I stead of '0'. Start assigning them with 0 and counting up.
Without an example sheet to use, I won't be able to do a whole lot of debugging for you.
However, Apps Script comes with it's own debugger. Select the function you wish you debug and click the Little bug icon beside the play button.
Click on the sidebar where you want to set a breakpoint, where the code will stop executing.
Once it hits that breakpoint you can see all the variables currently within your scope. So the array, value, and i variables are visible to you.
Use this to your advantage and debug your code to find out where the issue is. Alternatively, you can use Logger.log() to log values at certain points within your code and then read back through the logs to try and determine where the problem lies.
The problem is not with your if/else statement. The problem is with how you are assigning your variables from your row[] array. While you use regular numbers in the getRange() function, the range that is returned is an array of those cells. Arrays always start with an index of [0]. Change var multchoice = row[8] to var multchoice = row[7] and your if/else statement will work (you'll want to change all of your other references, too).
I am having to pickup from where someone in the business left off many years ago with an aging texting system.
It was built using ASP classic and sends a string to an API that then texts out, all this is neither here nor there. The problem i have is no JS experience, I am am a SQL Developer and did a little bit of ASP Classic (VBScript) years ago.
This piece of JScript picks up information from several form boxes and then places them in a string which is then passed to variable on a processing page to text out. The fields 'QValue, Indemnity and Excess' are all numeric. The Cover is text and it is replacing the cover text with 'NaN' now I understand this is for 'Not A Number' well that is exactly what it is, not a number but I want the text string.
Here is the snippet of code in question:
<script type="text/javascript">
function changeMessageText()
{
var messagetxt = document.getElementById('message').value
var QValue = document.getElementById('QValue').value
var Cover = document.getElementById('Cover').value
var Excess = document.getElementById('Excess').value
var Indem = document.getElementById('Indemnity').value
var messagetxt=messagetxt.replace("[QValue]", + QValue)
var messagetxt=messagetxt.replace("[Cover]", + Cover2)
var messagetxt=messagetxt.replace("[Excess]", + Excess)
var messagetxt=messagetxt.replace("[Indem]", + Indem)
document.getElementById('messageText').innerHTML = messagetxt;
}
</script>
Cheers.
When you do string.replace(searchvalue,newvalue), there is no need of + before the newValue
var messagetxt=messagetxt.replace("[QValue]", QValue)
//cover or cover2 whichever appropriate
var messagetxt=messagetxt.replace("[Cover]", Cover)
var messagetxt=messagetxt.replace("[Excess]", Excess)
var messagetxt=messagetxt.replace("[Indem]", Indem)
Is it normal that you use Cover2 in the replace where you read the input value and store it in the Cover variable ?
Those are two different variables and from the code you provided, we can only assume that Cover2 is initialized with NaN (which might not be the case, it can be copy/paste error).
Here is how you do it:
var messagetxt = document.getElementById('message').value;
var QValue = document.getElementById('QValue').value
var Cover = document.getElementById('Cover').value
var messagetxt=messagetxt.replace("[QValue]", QValue)
var messagetxt=messagetxt.replace("[Cover]", Cover)
document.getElementById('messagetxt').innerHTML = messagetxt;
Here is a working example of this: http://jsfiddle.net/F24cr/
Enjoy