Pass loop value along with a string - arrays

I want to move among the sheets within the same workbook, my sheet names are like 'Order 1', 'Order 2', 'Order 3'........so on. I want to take the value from loop along with 'Order' string like 'Order [i]' and want to paste some formula that also takes some value from the loop like ={Link!B[[i]+2]}
I tried with this following code but can't be succeeded.
function Order() {
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 1; i <= 10; i++) {
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Order [i]'));
spreadsheet.getRange('B3').activate();
spreadsheet.getCurrentCell().setValue('={Link!B[[i]+2]}');;
}
};

When you specify 'Order [i]', your script looks for a sheet called "Order [i]". Instead, you should write 'Order ' + i.
Similarly, '={Link!B[[i]+2]}' will return "={Link!B[[i]+2]}", just as you wrote it. Instead, write '={Link!B' + (i+2) + '}'.
Also, you don't need the .activate() or .setActiveSheet() calls. You can simply get the range and then immediately set the value. You're getting the sheet already with the .getSheetByName() method. In fact, you link all of it together.
function Order() {
var spreadsheet = SpreadsheetApp.getActive();
for (var i = 1; i <= 10; i++) {
spreadsheet.getSheetByName('Order ' + i)
.getRange('B3')
.setValue('={Link!B' + (i+2) + '}');
}
};

The string 'Order [i]' will always just be 'Order [i]', as everything is wrapped in quotes so the computer is interpreting it literally. You want to use something like 'Order ' + i, which will evaluate what i is and then append it to 'Order'.
In the second example, you might want something like '={Link!' + B[i+2] + '}'

Related

Matlab using editbox to match database

I'm facing problem when changing the user input using editbox when retrieve value from database.
The following is the working code.
conn = database('SQL', '', '');
name = input('what is your name: ', 's');
sqlquery = ['select Staff.staffPW from imageProcessing.dbo.Staff '...
'where Staff.staffID = ' '''' name ''''];
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
close(curs)
close(conn)
But now when I changed the input using editbox, problem occured
function pushbutton1_Callback(hObject, eventdata, handles)
conn = database('SQL', '', '');
name = get(handles.edit1,'String');
sqlquery = ['select Staff.staffPW from imageProcessing.dbo.Staff '...
'where Staff.staffID = ' '''' name ''''];
curs = exec(conn,sqlquery);
curs = fetch(curs);
curs.Data
close(curs)
close(conn)
I can get the correct pw from the working code, but the input from editbox I'm getting nothing. Anyone can teach me how to make it work? Thanks a lot!
The immediate issue is that the String property in your case is a cell array containing a string rather than just a plain string.
MATLAB edit-style uicontrols are capable of displaying multiple lines of text. To fill in multiple lines of text, in these cases, the string can be passed in as a cell array of strings where each element is on a different line of the edit box.
control = uicontrol('style', 'edit', ...
'Max', 2, ... % A two-line edit box
'String', {'Row1', 'Row2'});
Because of this, even for a single-line edit box, the String property could be a cell array of strings or just a string. Therefore, when retrieving values from an edit box, be sure to check if it is a cell array (iscell) or a string (ischar) before using it.
So adapting this to your code, we could do something like the following
name = get(handles.edit1, 'String');
% Check to ensure it is a cell array of one string
if iscell(name) && numel(name) == 1
name = name{1};
end
% Disallow non-strings, cell arrays of multiple strings, or empty strings
if ~ischar(name) || isempty(name)
error('A valid string must be supplied!');
end
sqlquery = ['select Staff.staffPW from imageProcessing.dbo.Staff '...
'where Staff.staffID = ' '''' name ''''];

Comparison of multiselect picklist with a field in text format in apex

I have an object with number of records in this format with Name in Text format -
Id Name
1 A,B,C
2 A,B
3 A
4 B
5 A,C
6 A,D
I have a multi-select picklist with values as:
A
B
C
D
So if I select B and C in my picklist, then I must have the records from the table in which name consists of B or C or (B and C) i.e. in this case, it should show 4 records:
1 A,B,C
2 A,B
4 B
5 A,C
I am using SQL query with IN and INCLUDES keyword but I am unable to achieve this functionality.
This depends on how many options you have for using soql. One way I can see it is:
Object__c myObject = new Object__c();
object.Name = 'A,B,D';
object.Multi_Select_Name__c = 'A;E';
insert object;
String query = 'select Id from Object__c';
//now we want to do a multi picklist, which is a String separated by ';'
List<String> nameSelections = object.Multi_Select_Name__c.split(';');
if(nameSelections.size() > 0) {
query += ' where ';
for(Integer i = 0; i < nameSelections.size(); i++) {
nameSelections[i] = 'Name like \'%' + nameSelections[i] + '%\'';
}
query += String.join(nameSelections, ' or ');
}
List<Object__c> objects = (List<Object__c)database.query(query);
This will create a query like:
'select Id from Object__c where Name like '%A%' or Name like '%E%'
Selecting all Object__c's where Name contains either A or E, generated from your multiselect.
Syntax errors may apply, I just wrote this real quick :) Ill double check myself.
EDIT:
This code works to build the query
String query = 'select Id from Object__c';
String multiSelect = 'A;E';
List<String> nameSelections = multiSelect.split(';');
if(nameSelections.size() > 0 ){
query += ' where ';
for(Integer i = 0; i < nameSelections.size(); i++) {
nameSelections[i] = 'Name like \'%' + nameSelections[i] + '%\'';
}
query += String.join(nameSelections, ' or ');
}
system.debug(LoggingLevel.INFO, query);

Save check constraint values to variables

I get a table check constraint definition this way:
select a.CHECK_CLAUSE
from INFORMATION_SCHEMA.CHECK_CONSTRAINTS a,INFORMATION_SCHEMA.TABLE_CONSTRAINTS b
where b.TABLE_NAME = 'table name'
For my example, suppose running this query returns this:
[([depname]='mathematics' OR [depname]='electronics' OR [depname]='computer science')]
How do I assign the values ​​specified in the check constraint into variables? i.e. computer science, mathematics and electronics?
It looks like you're getting a string returned. What you can do is split the string on instances of OR and store that in an array, and then run through the array and split each element on = to isolate the values. So, if you were to do this in PHP, the code might look something like this:
// For reasons of simplicity we will assume the result is stored in $result,
// and the leading [( and trailing )] have already been removed
$values = array();
$resultsplit = explode(' OR ', $result);
/* $resultsplit is now an array:
* $resultsplit[0] = "[depname]='mathematics'"
* $resultsplit[1] = "[depname]='electronics'"
* $resultsplit[2] = "[depname]='computer science'"
*/
if ($result != '') {
foreach ($resultsplit as $rs) {
$rsparts = explode('=', $rs);
/* $rsparts is now an array. On the first element:
* $rsparts[0] = "[depname]"
* $rsparts[1] = "'mathematics'"
* So all we need to do is stick $rsparts[1] into $values
*/
$values[] = $rsparts[1];
}
}
This will put all of the values into the array $values (including the single-quotes at the beginning and end) for you to do with as you please, regardless of how many there are. If PHP is not the language you have available to you, the same method should still work in your language of choice.

How to print values stored in a cell as strings (whole words) in Matlab?

For
A=[100;300;1000;240]
and
B=cell(8,1)
I have the following results stored in a B
[100]
[300]
[1000]
[240]
[100;300;240]
[100;1000]
[300;1000]
[100;300;1000]
I want to print these to display the output as :
choose first
choose second
choose third
choose fourth
choose first or second or fourth
choose first or third
.
.
etc
Basically, from the array A=[100;300;1000;240] , I want each value inside of it to be represented by a string, and not one variable. Any idea how to do this ?
note :
For my code, I want the user to input their own numbers in array A, and hence the length of A is variable and can be more than 4. The size of cell B also changes according to a formula, so it is not always fixed at size 8.
I would also appreciate a simple code, nothing too complex (unless necessary) as I don't have professional knowledge with matlab. A simpler code can help me understand and learn.
for mapping I would just use a map object
index_to_string = containers.Map(keySet,valueSet)
where
keySet = 1:20
valueSet = {'first'; 'second'; ...; 'twentieth'}
If A is available before printing, you can use the same valueSet, just cut it down to the size of A.
index_to_string = containers.Map(A,valueSet(1:length(A)))
Example:
G = cell(size(B))
for i = 1:length(B)
out1 = 'choose ';
if len(B{i}) == 1
out1 = [out1, index_to_string(B{i})];
else
temp = B{i}
for j=1:(length(temp)-1)
out1 = [out1, index_to_string(temp(j)), ' or ' ];
end
out1 = [out1, index_to_string(temp(end))];
end
G{i} = out1
end
Here's how I'd do it
function IChooseYouPikachu(Choices, Results)
% put in A for choices and B for results
%simple boolean to indicate whether a choice has been made already
answerChosen = 0;
for k = 1:length(Results)
Response = 'choose';
for m = 1:length(Choices)
if any(Results{k} == Choices(m))
if answerChosen
Response = [Response ' or ' NumToOrd(m)];
else
answerChosen = 1;
Response = [Response ' ' NumToOrd(m)];
end
end
end
fprintf('%s\n',Response);
answerChosen = 0;
end
function ordinal = NumToOrd(number)
switch number
case 1, ordinal = 'first';
case 2, ordinal = 'second';
case 3, ordinal = 'third';
case 4, ordinal = 'fourth';
otherwise, ordinal = 'out of index';
end
This answer is entirely based on JaredS's answer. I have just clarified your doubts.
Write this in some m-file.
Choices=A; Results=B;
%simple boolean to indicate whether a choice has been made already
answerChosen = 0;
for k = 1:length(Results)
Response = 'choose';
for m = 1:length(Choices)
if any(Results{k} == Choices(m))
if answerChosen
Response = [Response ' or ' NumToOrd(m)];
else
answerChosen = 1;
Response = [Response ' ' NumToOrd(m)];
end
end
end
fprintf('%s\n',Response);
answerChosen = 0;
end
Please write the following function in a separate file and put that in the same directory as the previous m-file. Then you should get an error saying: "Undefined function 'NumToOrd' for input arguments of type 'double'."
function ordinal = NumToOrd(number)
switch number
case 1, ordinal = 'first';
case 2, ordinal = 'second';
case 3, ordinal = 'third';
case 4, ordinal = 'fourth';
otherwise, ordinal = 'out of index';
end

Problems with string parameter insertion into prepared statement

I have a database running on an MS SQL Server. My application communicates via JDBC and ODBC with it. Now I try to use prepared statements.
When I insert a numeric (Long) parameter everything works fine. When I insert a string
parameter it does not work. There is no error message, but an empty result set.
WHERE column LIKE ('%' + ? + '%') --inserted "test" -> empty result set
WHERE column LIKE ? --inserted "%test%" -> empty result set
WHERE column = ? --inserted "test" -> works
But I need the LIKE functionality. When I insert the same string directly into the query string (not as a prepared statement parameter) it runs fine.
WHERE column LIKE '%test%'
It looks a little bit like double quoting for me, but I never used quotes inside a string. I use preparedStatement.setString(int index, String x) for insertion.
What is causing this problem?
How can I fix it?
Thanks in advance.
What are you inserting at '?'
If you are inserting
test
Then this will result in
WHERE column LIKE ('%' + test + '%')
which will fail. If you are inserting
"test"
Then this will result in
WHERE column LIKE ('%' + "test" + '%')
Which will fail.
You need to insert
'test'
Then this will result in
WHERE column LIKE ('%' + 'test' + '%')
And this should work.
I don't know why = "test" works, it should not unless you have a column called test.
I am using SUN's JdbcOdbcBridge. As far as I read yet, you should avoid to use it. Maybe there is a better implementation out there.
For now, I wrote the folling method. It inserts string-type parameters into the statement with string operations before the statement is compiled.
You should build a map of the parameters with the parameter index as the key and the value as the parameter itself.
private static String insertStringParameters(String statement, Map<Integer, Object> parameters) {
for (Integer parameterIndex : parameters.keySet()) {
Object parameter = parameters.get(parameterIndex);
if (parameter instanceof String) {
String parameterString = "'" + (String) parameter + "'";
int occurence = 0;
int stringIndex = 0;
while(occurence < parameterIndex){
stringIndex = statement.indexOf("?", stringIndex) + 1;
occurence++;
}
statement = statement.substring(0, stringIndex - 1) + parameterString + statement.substring(stringIndex);
}
}
return statement;
}

Resources