Google Script Custom Function simple IF statement not working - if-statement

I am new to Google Scripts. I have created a custom function script in my Google Sheets spreadsheet. I know the process works because I have see the data being passed to the function and data being returned.
The problem that I have is with the if statement. It is not finding "yes" even though there are multiple "yes" 's in the column. They all fail to the else. I even tried it with just the if and no else but not seeing any "hello"'s.
So can you help me understand why this if would not work? Why return input is returning the sent text but when inside the function it can't seem to qualify it?
Thank you for any help you can provide.
function DOUBLE(input) {
if (input == "yes") {
return "hello";
} else {
return input;
}
}

It works for me:
function xxDOUBLE(input)
{
if (input == "yes")
{
return "hello";
}
else
{
return input;
}
}
I put it in A1 as =xxDOUBLE(B1) and then went to B1 typed in 'yes' and got 'hello' back in A1. Typed in 'no' and got 'no' back in A1. I changed the name to xxDOUBLE because I wasn't sure if DOUBLE might be a real function because I don't use cell functions much.

Related

Unwanted string in while loop input to a file

Currently, I'm trying to input a users choice into a text file. I have done this part, but for my while loop, I have an exit option as a string. It also prints "exit" into the file and I can't figure out a way to stop it from doing this. getName simply gets the users input and getNum does the same but with numbers. I've tried finding a solution to this, but every attempt I've made hasn't been succesful. I'd really appreciate any feedback, as I'm quite new to coding.
while (m.menu == "1")
{
m.getName();
writeFile << m.name;
if (m.name == "exit")
{
break;
}
m.getNum();
writeFile << m.number;
if (m.number == "exit")
{
break;
}
}

The expression must be a modifiable l-value

I've been writing a simple data base in C++ using my basic object programming skills and I've come across a problem that I don't know how to get through. In one on my methods I'm trying to check correct forma of an input provided by a user. In order to achive that I need to know how long is the input string. Unfortunately an error pops out that the expression has to me modifiable l-value. I've searched for the answer, but I didn't really understand the solutions. Could you please, in layman terms, explain to me what did I do wrong?
Thank you!
The structure of the class I'm working with:
class Item
{
public:
void checkPNA()
{
if ((pna.length() = !6)||(pna[2]=!"-"))
{
cout<<endl<< "Niepoprawny format kody pocztowego! Poprawny format: \"00-000\". Spróbuj ponownie: ";
}
}
string nazwisko, imie, ulica, pna, miasto, attrib;
int id, len;
};
Simple syntax errors, it's != not = ! or =!, and it's '-' not "-" for a character.
if ((pna.length() = !6)||(pna[2]=!"-"))
should be
if ((pna.length() != 6) || (pna[2] != '-'))
You also don't need all those brackets
if (pna.length() != 6 || pna[2] != '-')
is easier to read in my opinion.

Gtk::TextView with constant string

I am using Gtkmm 3+ and What I am trying to do is have the text buffer have the constant string "> " even if the user tries to delete it. In addition when the user pressed return it will automatically be there again. Basically have a constant string like a terminal does.
The only way I can think about about accomplishing this would be to connect to the delete and backspace signals so the user cannot delete the string. But, is there a better way?
so far this is the only way I can think of:
//in constructor
txt_view_i_.signal_event().connect(sigc::mem_fun(*this, &MainWindow::inputEvent));
//function
bool MainWindow::inputEvent(GdkEvent* event)
{
if((event->key.keyval == GDK_KEY_BackSpace || event->key.keyval == GDK_KEY_Delete) && buffer_input_->get_char_count() < 3)
return true;
return false;
}
But doesn't work perfectly, because if you type in more then 3 characters then go to the beginning of the line you can delete the constant string.
Another way I just thought about was to add a label to the TextView widget. I did that but, the user could still delete it. Here is the code for that:
Gtk::TextBuffer::iterator it = buffer_input_->get_iter_at_line(1);
Glib::RefPtr<Gtk::TextChildAnchor> refAnchor = buffer_input_->create_child_anchor(it);
Gtk::Label* lbl = Gtk::manage(new Gtk::Label("> "));
txt_view_i_.add_child_at_anchor(*lbl, refAnchor);
This is very similar, but not quite identical, to the question I answered here: You can create a GtkTextTag that makes its contents uneditable, and apply it from the beginning of the buffer up to and including the "> " prompt.
Then when you receive input, append your output to the buffer and then append a new prompt on the next line, and re-apply the tag to make the whole thing uneditable.
The links in the linked answer show some C code where this is done, even including a prompt. It's not Gtkmm or C++, but it should serve as an illustration.
Here is the code I used to solve it:
Glib::RefPtr<Gtk::TextBuffer::Tag> tag = Gtk::TextBuffer::Tag::create();
tag->property_editable() = false;
Glib::RefPtr<Gtk::TextBuffer::TagTable> tag_table = Gtk::TextBuffer::TagTable::create();
tag_table->add(tag);
buffer_input_ = Gtk::TextBuffer::create(tag_table);
txt_view_i_.set_buffer(buffer_input_);
scroll_win_i_.add(txt_view_i_);
Gtk::TextBuffer::iterator buffer_it_ = buffer_input_->begin();
buffer_input_->insert_with_tag(buffer_it_, "> ", tag);
Here is how I made it so that the user cannot edit before the constant string:
//connect to the mark set signal
buffer_input_->signal_mark_set().connect(sigc::mem_fun(*this, &MainWindow::setMark));
//make the box uneditable
void MainWindow::setMark(const Gtk::TextBuffer::iterator& it, const Glib::RefPtr<Gtk::TextBuffer::Mark>& mark)
{
if(it.get_offset() < 2)
txt_view_i_.set_editable(false);
else
txt_view_i_.set_editable(true);
}
Hopefully someone will find this useful.

Weird cin behaviour

After some suggestions. I applied these.
Excerpt from a function bool getInput(int format=1)
cin>>num1>>plus>>num2>>i;
if(cin.fail()){
cout<<"\n[X][cin--fail] Oops! You entered in the incorrect format! Correct format: a+bi";
cin.clear();
cin.ignore(INT_MAX);
return false;
}
if(plus == '+' && i=='i'){
setComplex(num1, num2);
return true;
} else {
cout<<"\n[X] Oops! You entered in the incorrect format! Correct format: a+bi";
return false;
}
I've another function which calls the above mentioned function.
void getInput_recursive(int format=1){
while(!getInput(format)){};
}
It works fine for correct input.
But when the input is like 2+3iiiii then there is some weird thing happens.
My first guess: Maybe this was due to input buffer. I tried to clear it using cin.ignore and cin.clear but still nothing happened.
Can anyone help me out here? What am i doing wrong?
Please find the complete code here: http://codepad.org/Gl2zBrn1
(a) Don't recurse. while (!getInput(format)) {} will do, recursion is for saving state for later resumption and there's nothing to resume here.
(b) You're not returning a value on the false branch
bool getInput_recursive(int format=1){
if(getInput(format) == true){
return true;
} else {
getInput_recursive(format); // here
}
}
and that produces undefined behavior.
and (c) I think you need to look up what
cin.ignore(INT_MAX);
does, and what state that will leave your stream in. That'll do it.
Your approach is good, and you do the right operation but I think you have the order mixed up:
first cin.clear(); to reset the flags, than cin.ignore(INT_MAX); to clear the buffer.
EDIT:
I think you need to replace cin.ignore(INT_MAX); with cin.ignore(INT_MAX,'\n');
Let me know if this does the trick.

Having trouble getting GET variables from url

I'm having trouble with this algorithm to extract the get variables from a url and print them each on a new line like:
x=y
z=hello
etc. but instead it prints a seemingly random section of the url with no newlines to the file. There must be a logic error of some kind but i just can't spot it.
for(i_m=0;i_m<len_m;i_m++) {
if(var_m[i_m]=='&') {
fwrite(var_m+offset_m, 1, amp_m, echo_out);
fputc('\n',echo_out);
offset_m+=amp_m;
amp_m=0;
}
amp_m++;
}
any help appreciated.
EDIT:
thank you everyone for your comments, I corrected that error Guss but to no avail. I thought up another algorithm, since I can't use c++ strings in this one
while((i_m=(strchr(var_m,'&')-var_m))>0) {
var_m[i_m]='\n';
}
Which would change each of the & to a newline, and then I could just write var_m to the file, but for some reason this gives me a buffer overflow.
void StringExplode(std::string &str, const std::string &separator, std::vector<string>* results){
int found;
found = str.find_first_of(separator);
while(found != string::npos){
if(found > 0){
results->push_back(str.substr(0,found));
}
str = str.substr(found+1);
found = str.find_first_of(separator);
}
if(str.length() > 0){
results->push_back(str);
}
}
your accounting of offset_m and amp_m seems to be wrong. Take for example the simple string "a&b&c" - your code should have outputed from that the text:
a
b
c
but if you trace through the code you will see that when you get to the first & then offset_m=0 and amp_m=1 and you'd print a - which is Ok but when you get to the second & then offset_m=1 and amp_m=2 which would actually print &b and at no point you'd print the last element.
Using a simple string splitting algorithm like mysqlforums suggested is a common way to handle this task, but I believe you should be able to come up with a simple loop algorithm that will get you what you need. think about it again and try to run through the algorithm in your head (or using pen and paper) to try to understand how it works - I'm sure you'll get it!
If you're still having problems, post something here and I'll try to help again.