Google Sheets IMORTHTML converting to scientific notation - google-sheets

I am importing a table to google sheets with a formula like:
=IMPORTHTML("http://TheServer/FetchBatchData.php,"table",1)
In a web browser I get data that looks like the following:
Batch Batches Mix Customer Status
30DE 3 AGPDHIGH Joe Sent
30DF 3 KHM100 Nancy Sent
30E0 1 DEER100 Bob Sent
30E1 3 KHM100 Ted Sent
My problem is that Google Sheets converts the Batch data (a simple Hex value) to scientific notation when it sees the "E" in the batch. The result is that "30E0" becomes 30 and "30E1" becomes 300. I have tried changing cell formats with no luck. Is there something that I am missing in setting up the formatting or the formula that can get Google to not help me?

I made several tests using formulas. The values like "30E0" are always taken and calculated automatically, even with the fix in the comments.
Because of that, the only way I found to fix the issue was to create a custom formula that takes the values of the HTML and returns plain text automatically, which will fix the issue.
The custom formula was created based on the URL in the sample sheet "https://ex1.svfeeds.com." However, you can change it to the correct one.
Here is the sample code:
function parseXml() {
// URL where the data is located and calling the URL fetch services.
let url = "https://ex1.svfeeds.com/";
let xml = UrlFetchApp.fetch(url).getContentText();
let document = XmlService.parse(xml);
let root = document.getRootElement();
// path to reach the raw data
let body = root.getChild('body');
let table = body.getChild('table');
let tbody = table.getChild('tbody');
let rows = tbody.getChildren('tr');
// empty variable, it will be updated with the new array
// with the data of the table
let tableData = [];
// starts the for loop to each of the rows and columns of the HTML table
for (let i = 0; i < rows.length; i++){
var columns = rows[i].getChildren('td');
if (columns.length == 0){
columns = rows[i].getChildren('th');
}
// empty variable, it will be updated with the new array
// with the information in the rows
var rowValues = []
// new "for" that will call the values in each cell in the table
for (var j = 0; j < columns.length; j++){
var cell = columns[j].getValue();
// push the cell values to the rows
rowValues.push(cell);
}
// push the values to the complete table
tableData.push(rowValues);
}
return tableData;
}
After that, you can call the custom function by adding =parseXml() to A1 for example, like this:
And it will look like this:
The path of the values use in this sample code, can be change base on how is the information is place in the original link. For example for the current page we use these variables to get to the raw data:
// path to reach the raw data
let body = root.getChild('body'); // 1
let table = body.getChild('table'); // 2
let tbody = table.getChild('tbody'); //3
let rows = tbody.getChildren('tr'); //4
and
columns = rows[i].getChildren('th'); //5
Base in the HTML source:
Reference:
Apps Script XML Service.
Custom Formulas.
Let me know if you have further questions.

I found a workaround though it is not something that I really like. Since the google Sheets are the only thing currently reading this table, I prepended a single quote to the output, so the 30E1 data becomes '30E1, and that will get me by until I can get Google Sheets to stop reformulating the data.
I'm still interested in more better if anyone has it.

Related

ImportXML function in Google Sheets to pull out data from first column

I am trying to import data from the first column of this web table, using Google Sheets ImportXML and ARRAYFORMULA functions
So I want to get a column with these numbers:
007679961
007679962
007679963
....
007829037
Table content is constantly being updated and number of rows keeps growing, rows are regularly being added to the end or inserted somewhere in the middle. The structure of the table is constant.
Could someone suggest a good formula for my case?
Too big to use importhtml, so, try this simplified solution
=import1stColumn(A1)
with
function import1stColumn(url) {
var source = UrlFetchApp.fetch(url).getContentText().replace(/(\r\n|\n|\r|\t| )/gm,"");
var regex = /(<([^>]+)>)/ig;
var data = [];
var html = source.substring(source.indexOf('<TABLE'),source.indexOf('</TABLE'))
var trs = html.split('<TR')
for (var i=2;i<trs.length;i++){
var tds = trs[i].split('<TD')
data.push([('<TD'+tds[1]).replace(regex,"")])
}
return(data);
}

Newbie: writing a script for google sheets that will run on all sheets

I am a real novice at this (not even). Have never written code in my life-just copied the following code off a website that explained how to do it. I a google doc that I need to be sorted according to Column C continuously as new data is added. This is the code I was given:
function onEdit() {
var sheet = SpreadsheetApp.getActiveSheet();
var range = sheet.getRange(3, 1, sheet.getLastRow() - 2, sheet.getLastColumn());
range.sort({column: 3, ascending: true});
}
It works partially, but I need it to be applied to all sheets/tabs in the document.
Thanks in advance!
Your code includes the line var sheet = SpreadsheetApp.getActiveSheet(); and the rest of you code is based on sheet - so it run only on one sheet, the one that is active when new data is inserted, so the one in which the edit is performed
If instead you want to perform the sorting on each edit in all sheets of the spreadsheet, you need the method getSheets() to retrieve all sheets of the spreadsheet as an array
Subsequently you need to loop through the array to apply the sorting function to each sheet
Sample:
function onEdit() {
var sheets = SpreadsheetApp.getActive().getSheets();
for (var i = 0; i < sheets.length; i++){
var sheet = sheets[i];
var range = sheet.getRange(3, 1, sheet.getLastRow() - 2, sheet.getLastColumn());
range.sort({column: 3, ascending: true});
}
}
Important:
The onEdit trigger only detects human-made edits like typing of text. If instead you want to fire the trigger on changing the document structure, such as e.g. inserting a new empty row (which is different from typing values into cells of an alreday existing empty row), you would need to replace the onEdit trigger, through the installable onChange trigger, see also here

How to use value of a cell once?

I have a certain cell (e.g. C1) that is computed by some formula and another cell (e.g. C2) that when a IF event happens, it takes the value of cell C1.
But, I only want to have C2's value be calculated one initial time and from then on, C2's value will stay the same and no longer depend on C1.
Is that possible, and if so, how can I do that?
If I wasn't clear enough, here is an example spreadsheet with the logic commented.
Found the answer: a simple copy script can do the job:
function simpleCopy() {
var sheet = SpreadsheetApp.getActiveSpreadsheet(); //access to the spreadsheet
SpreadsheetApp.setActiveSheet(sheet.getSheetByName('testing enviroment')); //access to the sheet by name
var range = sheet.getRange('D1'); //assign the range you want to copy
var copy = range.getValues();
sheet.getRange('D2').setValue(copy) //new range you want to paste a value
}

Duplicate Sheet with Name of Cell Value

I am wondering if there is a way to duplicate a sheet I am working on and assigning that new duplicate sheet a name based on a cell value in the original sheet?
Right now I am simply using a script/button to clear content of cell values to start a new stock order (see below)
function newSalesOrder() {
var activeSheet = SpreadsheetApp.getActive();
var ranges = ["StockOrder!B4", "StockOrder!E8", "StockOrder!D9","StockOrder!A12:B41"];
for (var i = 0; i < ranges.length; i++) {
activeSheet.getRange(ranges[i]).clearContent();
}
}
So I would like to add the ability to duplicate the StockOrder! sheet, rename it to the Purchase Order Number value in Cell C4 for example.
If I understand correctly what you are trying to achieve, all you need to do is:
Save the content of a given cell into a String variable
This one is a bit tricky, the hierarchy in Google sheets is: SpreadSheet: Sheet: Range: Cell. So what you need to do to get the value of Cell C4 is:
var ss = SpreadsheetApp.openById('Insert ID here');
var name = ss.getName();
var sheet = ss.getSheetByName('Insert Sheet Name Here');
var range = sheet.getRange(3,4);
var newSheetName = range.getValue();
Of course, you do not need to use openById or getSheetByName methods. You can use getActiveSheet() or whatever you like.
Create a new sheet whose title is the saved String variable
var newSpreadSheet = SpreadsheetApp.create(newSheetName);
Hope this is gonna help:)

How do I get value of cell before it changes in Google spreadsheet?

This function seems not to be triggered until the cell is exited, as the value i'm getting is the post-change value. I need the pre-change value, but I only need it if in fact that particular column is edited.
Thank you
function onEdit() {
var s = SpreadsheetApp.getActiveSheet();
var r = s.getActiveCell();
var columnNum = r.getColumn();
var msg;
if (columnNum == 11) {
var dateCell = s.getRange(r.getRow(), 11);
var v=dateCell.getValue();
msg = 'Value= ' + v;
Browser.msgBox (msg);
//dateCell.setValue(v);
}
}
This is a very old question, but in case anyone else needs the answer, it's quite easy to get both the old and new values of any edited cell with the event object:
function onEdit(e) {
let oldValue = e.oldValue;
let newValue = e.value;
console.log("The range's old value was: " + oldValue + ", and the updated value is: " + newValue);
}
Here are are the accessible properties of the Google Sheets event objects.
To the best of my knowledge, capturing the value of a cell before an edit is still not possible.
You might like to star this issue on the issue tracker; it is currently deemed a medium-priority enhancement request.
#AdamL Is there anyway to access the spreadsheets revision history programmatically?
Sorry this is beyond my noob abilities, but my question may hold the clue to a workaround for A B's issue.
#A B ANOTHER POSSIBLE WORKAROUND
You could set up a time based triggers to populate 2 arrays. 1 array from the current data in the column your checking for edits, and 1 array from a previous snapshot of the same data. Then by using a loop to test if any values have changed since your last trigger event, you can determine what value/values have changed(if any) If a value your testing for has changed then push the rowNumber() and colNumber() and getValue() of both previous and current instances of the same data to a new array, also include a timestamp new Date(). Have this new array data populate a new sheet or spreadsheet.
If used in conjunction with your onEdit function, yourll be able to see the before and after value changes, and the approximate time they occured.
Time based triggers can be set to run every minute, hour day etc

Resources