This question already has answers here:
Javascript - String concatenation [duplicate]
(2 answers)
How can I do string interpolation in JavaScript?
(21 answers)
Closed 2 years ago.
I set formula in sheet1 cell (1,9) to get the value from other sheet through google apps script. Where "sheet" is a variable, I need to get value from specific sheets. The code below shows "=sheet!B1" rather than "=6!B1".
var ss1 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/xxxxxxxxxxxxxxxxxxxx/edit#gid=0");
var sheet1 = ss1.getSheetByName("Sheet1");
var sheet;
sheet = 6;
var sheet = sheet.toString();
sheet1.getRange(1,9).setFormula('sheet!B1')
}
Google Apps Script uses JavaScript as programming language
setFormula requires a string as parameter.
Considering the above you could use any string manipulation technique available using the JavaScript version supported by the runtime that your project is using.
Among other techniques you coul use:
String concatenation operator i.e. sheet + '!B1' (if you are using the old (Mozilla Rhino) or the new runtime (Chrome v8) )
String templates i.e. `${sheet}!B1` (only if you are using the new runtime (Chrome V8) )
Related
This question already has answers here:
Does a JavaScript bookmarklet need to be a valid URL?
(2 answers)
How to create a URL from a string that replaces special characters?
(1 answer)
Closed last month.
This post was edited and submitted for review last month and failed to reopen the post:
Original close reason(s) were not resolved
I wrote a relatively simple program designed to calculate annual compound interest, and while it worked in google chrome's console, when I converted it into a bookmark it would still give me the prompts, yet not the final result. I was wondering if I am supposed to format it differently, or if this is impossible.
Here is my code:
var pv = prompt('principal');
var rate = prompt('rate');
rate = rate/100;
var time = prompt('time');
var fv = pv*(1 + rate)**time
alert(fv);
As a bookmarklet it will all be on a single line, so you need to have a semicolon after this line:
var fv = pv*(1 + rate)**time;
Then it will work.
I made an application with google drive and sheets api the problem is that the sheet has some formulas that has conflicts with the created api so I was forced to erase the formulas and write a little app script to autofill the formulas. I was following this [tutorial][1]. But as the formula has strings to concat it throws a syntax error. My code is here:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("I2").setFormula(CONCAT("problem string";"\n";C2;"\n";F2));
}
Do i need to declare the string as a variable?
EDIT: i am also having problems when using the \n new line character
[1]: https://www.youtube.com/watch?v=cCBtsQGtzoQ
Issues:
setFormula accepts a string but you are passing CONCAT which is not defined nor it is a built in JavaScript method. To make it a string directly, you can use template literals.
also your formula is wrong because CONCAT can concatenate only 2 strings, not 5. You need to use CONCATENATE.
Solution:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
ss.getRange("I2").setFormula(`CONCATENATE("problem string";"\n";C2;"\n";F2)`);
}
Output:
This question already has answers here:
How do I parse a URL into hostname and path in javascript?
(26 answers)
Closed 2 years ago.
I have a bug in my wysiwyg editor.
I have a box where I past a tweet url, and I must extract the ID to replace it with the embed code.
regex: {
twitter: /https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/
}
if (str.match(this.opts.regex.twitter)) {
parsed = str.replace(this.opts.regex.twitter, '<blockquote class="twitter-tweet tw-align-center">TWEET $1</blockquote>');
return parsed;
}
The bug is in my regex
/https?:\/\/twitter.com\/[a-zA-Z_]{1,20}\/status\/([0-9]*)/
If I take this url
https://twitter.com/howard3141/status/1330546000969273347
It doesn't work because 'howard3141' has some numbers inside.
Since you need to fetch from a URL, window.location provides great apis to get value.
Following is the demonstration of it.
Note: I'm using URL to create a location like object, but it should work with window.location as well
var url = new URL('', 'https://twitter.com/howard3141/status/1330546000969273347')
console.log(url.pathname.match(/\/([^\/]*)\//)[1])
This question already has answers here:
Remove first character from a string if it is a comma
(4 answers)
Closed 3 years ago.
My website uses product references in this style: "R202020"
I want them to be shown like this for the users of my website: "BA2202020"
So basically I'm looking for a script, which formats the style of my reference numbers (should affect a ".reference" class I've created) by:
Removing the "R" in the original reference - replacing it with a "BA2" in stead - leaving the rest as it is (the "202020" part).
How can I do this?
Find 1st character of your string using string[0] and replace that with your desire value like below.
var string=$('.YourClass').text();
var result = string.replace(string[0],'BA2');
$('.YourClass').text(result);
console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<span class='YourClass'>R202020</span>
Try replace method. https://www.w3schools.com/jsref/jsref_replace.asp
'R202020'.replace('R2','BA2') // BA202020
This question already has answers here:
Why would a JavaScript variable start with a dollar sign? [duplicate]
(16 answers)
Closed 7 years ago.
What is a difference between these two variables? (please see dollar sign)
var $button = $("<input>");
and
var button = $("<input>");
?
It just a developer taste. Suppose you want to save a HTML element in a variable in your program you do this.
var $input = $('input');
So when ever we see the variable $input we are sure that this hold a jQuery object (the html element on which jquery library functions can be performed on eg: $input.toggle(), $input.remove(), $input.val() etc).
where the normal variable name without $ are to point out program variables. For Example we can say variables which holds some values that cant perform math operations. like
var shippingCost = 40;
var quantity = $('#totalQuantity').val();
var totalCost = $('#totalCost').val();
var finalPrice = (totalCost * quantity )+ shippingCost;
So you see here we do more of a math operations.
Also the usage of $ in variable names is purely the developers interest it all bubbles up to the code readability. Its just one of the Jquery Beast Coding Practices.
There is no real difference between these two variables. It's just a convention to start the names of variables which contain jQuery objects with a dollar sign. This makes it easier to distingiush them from variables containing HTML elements, for example.