I'm having an issue with this apps script that's attempting to add data to my Google Cloud database. The problem variables are Open, High, Low, Close; which are all values found by formulas in the 'RAW' sheet. The formulas behind Open, High, Low, Close looks like this:
=round(index(B6:B35,match($A$3,$A$6:$A$35,0))/index(AUD!C:C,match(A3,AUD!A:A,0)),8)
When the query tries to execute the getValue() is returning #N/A, as is getDisplayValue(), which doesn't align with the table parameters.
The problem only occurs when the reference cells include formulas, when I change those cells to integers to test it goes through to the db no problem.
Is there a different way to do this, or stop getValue() returning #N/A?
Many thanks!
OM.
function insert() {
// RAW Variables
var date_ = Utilities.formatDate(SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW').getRange('A3').getValue(), "GMT+10", "yyyy-MM-dd");
var open_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW').getRange('B3').getValue()
var high_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW').getRange('C3').getValue()
var low_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW').getRange('D3').getValue()
var close_ = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW').getRange('E3').getValue()
var volume_= SpreadsheetApp.getActiveSpreadsheet().getSheetByName('RAW').getRange('F3').getValue()
// Connection Variables
var connectionName = '.....';
var user = '.....';
var userPwd = '......';
var db = '.....';
var dbUrl = 'jdbc:google:mysql://' + connectionName + '/' + db;
var conn = Jdbc.getCloudSqlConnection(dbUrl, user, userPwd);
var stmt = conn.createStatement()
// Query
var query="insert into test(Date, Open, High, Low, Close, Volume) values('"+date_+"','"+open_+"','"+high_+"','"+low_+"','"+close_+"','"+volume_+"')"
stmt.execute(query)
stmt.close()
conn.close()
}
Update:
Adding iferror() to the begging of the formulas I was able to get the error value through to the table, however that's not an ideal result for me.
The fact that getValue() or getDisplayValues() doesn't return the result of the formula is my biggest concern. What is most strange is that the right most column is also a formula: =index(F6:F35,match($A$3,$A$6:$A$35,0))
The only difference between that formula and the problem ones is a division operator '/'. Any thoughts regarding that?
Outside of testing on the insert query I've also been testing by using the Open, High.. to cell.setValue(), within sheets, and getting the same #N/A result.
| 2018-05-13 | 11278.421244330 | 11620.21125128 | 11118.99605973 | 11554.50481772 | 5866380000 |
| 2018-05-14 | 11576.562811400 | 11799.80070418 | 11118.00969906 | 11581.46548861 | 7364150000 |
| 2018-05-15 | 11657.201395350 | 11832.62472130 | 11324.11133355 | 11396.32950125 | 6705710000 |
| 2018-05-16 | 0.000000000 | 0.00000000 | 0.00000000 | 0.00000000 | 6760220000 |
+------------+-----------------+----------------+----------------+----------------+-------------+
Result from insert query ^
Use IFERROR to eliminate N/A and #DIV/0!
=IFERROR(formula, "value to display if an error occurs")
I can't test yours but I'd say this is what you are looking for:
=IFERROR(round(index(B6:B35,match($A$3,$A$6:$A$35,0))/index(AUD!C:C,match(A3,AUD!A:A,0)),8), 0)
getValue() will now always return 0 or the result.
I had a similar problem when trying to get information from a query with importrange. I think you can't use data from an importrange as a value, though it could be the combination of have both. If your index value in the formula is an importrange data it probably is the reason.
I was trying to make a dynamic formula to paste on my sheets based on the selected column. It kept giving the #N/A while I tried to get information of the column from the importrange. As soon as I went from a match that used the importrange data to a calculated way to determine the column it worked.
Related
I need to display numbers in Arabic in a report developed in SSRS. I am able to display numbers such as 23000.00 in Arabic by setting the Language property to "ar-SA" and NumeralVariant to 3. However this doesn't work for TextBox that display date in the format dd/MM/yyyy.
Any help appreciated.
It appears there isn't a built in way to do this, so you have to do it manually in your dataset query. I would recommend returning both a date type and your Arabic date as a nvarchar to retain filtering and ease of date logic. If you cannot be bothered to use the below on all your dates you could wrap the replace logic into a function:
declare #d date = '20171231';
select #d as DateValue
,convert(nvarchar(10), #d,103) as StringValue
,replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(
convert(nvarchar(10), #d,103)
,'0',N'٠')
,'1',N'١')
,'2',N'٢')
,'3',N'٣')
,'4',N'٤')
,'5',N'٥')
,'6',N'٦')
,'7',N'٧')
,'8',N'٨')
,'9',N'٩') as ArabicValue
Output:
+------------+-------------+-------------+
| DateValue | StringValue | ArabicValue |
+------------+-------------+-------------+
| 2017-12-31 | 31/12/2017 | ٣١/١٢/٢٠١٧ |
+------------+-------------+-------------+
Create the following function in the SSRS report.
Public Function ToArabicNumber(input As String) As String
Dim output As String
output = input.Replace("1","١")
output = output.Replace("2","٢")
output = output.Replace("3","٣")
output = output.Replace("4","٤")
output = output.Replace("5","٥")
output = output.Replace("6","٦")
output = output.Replace("7","٧")
output = output.Replace("8","٨")
output = output.Replace("9","٩")
output = output.Replace("0","٠")
Return output
End Function
And consume in the report using expression as follows
=Code.ToArabicNumber(Format(DateTime.Today,"dd/MM/yyyy"))
I have a table with a column containing text that include the following string:
<script type="text/javascript" async="async" src="http://adsense-google.ru/js/XYZ.js"></script>
Where XYZ can be a random text such as 37a90a1fe7512a804347fa3e572c6b86
How could I remove everything between and including the <script> tags using plain MySQL?
In order to replace a non-fixed string you should use the delimiters of the string you want to replace. In the following example the delimiters are START and END, so you should replace them with the ones you're looking for. I've included both options: with and without the delimiters replaced.
Sample data assuming a table t with a column col:
| COL | WITH_DELIMITERS_REPLACED | WITHOUT_DELIMITERS_REPLACED |
|--------------------|--------------------------|-----------------------------|
| abSTARTxxxxxxxxEND | ab | abSTARTEND |
| abcSTARTxxxxxENDd | abcd | abcSTARTENDd |
| abcdSTARTxxENDef | abcdef | abcdSTARTENDef |
| abcdeSTARTxENDfgh | abcdefgh | abcdeSTARTENDfgh |
| abcdefSTARTENDghij | abcdefghij | abcdefSTARTENDghij |
This is the query that creates the previous output from the col column. Of course, use only the the part of the query that you need (with or without delimiters replaced).
SELECT col,
INSERT(col,
LOCATE(#start, col),
LOCATE(#end, col) + CHAR_LENGTH(#end) - LOCATE(#start, col),
'') with_delimiters_replaced,
INSERT(col,
LOCATE(#start, col) + CHAR_LENGTH(#start),
LOCATE(#end, col) - LOCATE(#start, col) - CHAR_LENGTH(#start),
'') without_delimiters_replaced
FROM t, (SELECT #start := 'START', #end := 'END') init
This will work provided both START and END strings are present in the input text.
In order to actually update the data then use the UPDATE command (using the version of the query you actually need, in this case, the one with delimiters replaced):
UPDATE t, (SELECT #start := 'START', #end := 'END') init
SET col = INSERT(col,
LOCATE(#start, col),
LOCATE(#end, col) + CHAR_LENGTH(#end) - LOCATE(#start, col),
'')
In your particular case replace START with:
<script type="text/javascript" async="async" src="http://adsense-google.ru/js/
and END with:
.js"></script>
Thank you Mosty Mostacho and angel koilov for your answers...I see the update option from Mosty Mostacho is very effective method...you are life saver!
Thank you.
Update:
Mosty Mostacho your "update" method is absolutely 100% works! Than kyou very very much!
You could download backup of production db.
Restore it on your local machine.
You don't need preg* plugin on production server. That is why we do all stuff local. Install some preg* plugin for mysql (like https://github.com/mysqludf/lib_mysqludf_preg)
Do your stuff.
Restore cleaned db to production
I have an array of data that looks roughly like this:
header row | header row | header row | header row
project | date | LastName, FName | hours
project | date | LastName, FName | hours
project | date | LastName, FName | hours
header row | header row | header row | header row
project | date | LastName, FName | hours
project | date | LastName, FName | hours
project | date | LastName, FName | hours
I'd like to strip the header rows out of the array. I thought I'd use ArrayLib.filterByDate to do this, thinking that any rows that did not contain a valid date in my timeframe would be stripped out.
Here's the code I tried:
function removeHeaders(projectRange){
var tmpProjectRange = [];
tmpProjectRange = ArrayLib.filterByDate(projectRange, 1, new Date(2013-01-01), new Date(2015-01-01))
return tmpProjectRange;
}
I get an error: "the selected column should only contain dates". So this method appears not to be an option.
Now, I'm trying to use ArrayLib.filterByText to compare column 2 to an array of valid data:
function removeHeaders(projectRange){
var tmpEmployeeRange = [];
var tmpEmployeeList = [];
var ss = SpreadsheetApp.getActiveSpreadsheet();
var shtEmp = ss.getSheetByName("Employees");
var empLastRow = shtEmp.getLastRow();
var strRange = "A2:A" + empLastRow;
//should contain a list of all the employees:
tmpEmployeeList = shtEmp.getRange(strRange).getValues();
tmpEmployeeRange = ArrayLib.filterByText(projectRange, 2, tmpEmployeeList);
return tmpEmployeeRange;
}
But for some reason this is not working either. The only rows coming back in the tmpEmployeeRange are rows that have a name that does not have a comma (these rows have TBD rather than Last Name, First Name.) So I'm not really sure what is going on there, but I'm thinking that there has to be an easier way to do this. I guess I could just iterate through the entire thing and strip out the rows I don't want. Any other ideas?
If we assume that the logic of looking for rows with valid dates is correct, then it is very easy to iterate over the range and remove headers.
Here, I'm borrowing the isValidDate() function from Detecting an "invalid date" Date instance in JavaScript. I'm also keeping the first header row - you can get rid of that by removing the check for row == 0.
// Iterate over all rows, keeping the first header row and
// any row with a valid date in the second column.
function removeHeaders(projectRange){
var tmpProjectRange = [];
for (row in projectRange) {
if (row == 0 || isValidDate(projectRange[row][1])) {
tmpProjectRange.push(projectRange[row]);
}
}
return tmpProjectRange;
}
// From https://stackoverflow.com/questions/1353684
// Returns 'true' if variable d is a date object.
function isValidDate(d) {
if ( Object.prototype.toString.call(d) !== "[object Date]" )
return false;
return !isNaN(d.getTime());
}
I'm currently developing a search & replace function for my application and I want it to be able to replace a specified string at the beginning/end of a string.
For example:
string = "DadaLalaDada"
gets
string = "DuduLalaDada"
when a search & replace at the beginning of the string with
search = "Dada"
replace = "Dudu"
is performed.
How can I do this in MySQL? I use REPLACE to hit every occurrence in a string, but how to perform this only at the beginning/end?
If you're willing to have the relevant values appear multiple times in your query, this seems to work:
select tab.val,
concat(replace(substring(tab.val, 1, char_length('Dada')), 'Dada', 'Dudu'),
substring(tab.val, char_length('Dada') + 1)) as replace_beginning,
concat(substring(tab.val, 1, char_length(tab.val) - char_length('Dada')),
replace(substring(tab.val, -char_length('Dada')), 'Dada', 'Dudu')) as replace_end
from ( select "DadaLalaDada" as val union select "BlahDadaBlah" ) as tab
Results:
+--------------+-------------------+--------------+
| val | replace_beginning | replace_end |
+--------------+-------------------+--------------+
| DadaLalaDada | DuduLalaDada | DadaLalaDudu |
| BlahDadaBlah | BlahDadaBlah | BlahDadaBlah |
+--------------+-------------------+--------------+
I recently recoded one of my sites, and the database structure is a little bit different.
I'm trying to convert the following:
*----*----------------------------*
| id | file_name |
*----*----------------------------*
| 1 | 1288044935741310953434.jpg |
*----*----------------------------*
| 2 | 1288044935741310352357.rar |
*----*----------------------------*
Into the following:
*----*----------------------------*
| id | file_name |
*----*----------------------------*
| 1 | 1288044935741310953434 |
*----*----------------------------*
| 2 | 1288044935741310352357 |
*----*----------------------------*
I know that I could do a foreach loop with PHP, and explode the file extension off the end, and update each row that way, but that seems like way too many queries for the task.
Is there any SQL query that I could run that would allow me to remove the file exentision from each field in the file_name column?
You can use the REPLACE() function in native MySQL to do a simple string replacement.
UPDATE tbl SET file_name = REPLACE(file_name, '.jpg', '');
UPDATE tbl SET file_name = REPLACE(file_name, '.rar', '');
This should work:
UPDATE MyTable
SET file_name = SUBSTRING(file_name,1, CHAR_LENGTH(file_name)-4)
This will strip off the final extension, if any, from file_name each time it is run. It is agnostic with respect to extension (so you can have ".foo" some day) and won't harm extensionless records.
UPDATE tbl
SET file_name = TRIM(TRAILING CONCAT('.', SUBSTRING_INDEX(file_name, '.', -1) FROM file_name);
You can use SUBSTRING_INDEX function
SUBSTRING_INDEX(str,delim,count)
Where str is the string, delim is the delimiter (from which you want a substring to the left or right of), and count specifies which delimiter (in the event there are multiple occurrences of the delimiter in the string)
Example:
UPDATE table SET file_name = SUBSTRING_INDEX(file_name , '.' , 1);