I've just learnt about arrayformula and continue for keeping formulas into new rows, now im trying to get a single string to copy into new rows "N". How can I achieve this?
Basically, i've got some columns with formulas that are copied down using array formula, i've tried to use this to copy "N" into a column which needs to be pre-filled with "N" how it has not work, is there any other ways I can achieve this?
Overall I'm just asking is there a way to fill a single column with the letter "N" using either a formula or a script? If yes how?
Many thanks
The most efficient way using a script would be like this :
function myFunction() {
var s = SpreadsheetApp.getActiveSheet();
var col = [];
for(var n=0 ; n<s.getMaxRows();n++){
col.push(['N']);
}
s.getRange('N:N').setValues(col); // write data in any column, N in this example :-)
}
It creates a 2D array with the right value and size and writes it on the sheet in one single step.
Don't have a spreadsheet at hand to try, but try
= arrayformula(if(N:N=0;"N";"N"))
Related
Not sure if this is going to make sense but here I go.
What I want to do is to create a formula that isn't linked to a cell directly. In example: if I want to calculate carryweight for a tabletop game like D&D I would need the formula (strengthBonus x 5). For my current attempt I renamed the range (cell rather) strengthBonus to MOD_STR so when I put the formula =(multiply(MOD_STR,5) it works like a charm. Then I named that range "CARRYWEIGHT" and then use it elsewhere.
What I would like to be able to do is to make a new variable, similar to the way that "Define Named Range" does, but instead of relying on the variables being somewhere on the spreadsheet they would process from an internal formula. For example, if I type =carryweight into a cell it would run the equation =MULTIPLY(MOD_STR,5) in that cell and output the answer. I know nothing about code yet but have just been pointed in the direction of tutorials but I'm also asking for help here.
The code I have tried is
function CARRYWEIGHT(MOD_STR){
return MOD_STR*2}
and something else, I can't remember what but I got it to at least accept it in the spreadsheet. When I type it in I get an error stating that the outcome isn't a number.
I have no idea where to go from here.
Thank you in advanced for your help.
The difference between sheets formulas and Apps Script is that in Apps Script you need to retrieve the value of the range corresponding to the name of a named range
You cannot simply multiply the name of the range (which is a string!) with a number
Here is a sample of how to retrieve a range by name and make calculations wiht the value stored in it:
function CARRYWEIGHT(MOD_STR){
// retrieve all named ranges in the spreadsheet
var namedRanges = SpreadsheetApp.getActive().getNamedRanges();
//loop through all the results
for (var i = 0; i < namedRanges.length; i++){
var range = namedRanges[i];
//if the range with the name equal to the value of MOD_STR is found, get the cell content of this range
if(range.getName()==MOD_STR){
var value = range.getRange().getValue();
// perform the calculation with the cell content of the named range
return value*2;
}
}
}
From the cell, call the function as =CARRYWEIGHT("paste here the name of the range of interest"), do not forget the quotes (unless it is a cell reference)!
I hope this helped you to get started, for further understanding plese consult the following references.
References
Named Ranges
Loops
Conditional statements
Ranges
getValue()
I am trying to learn Google Script. I want to write a script that uses a For Loop to save the value of each cell, clear the cell, then paste that saved value back into it. The data is in cells C6 - C17. So far, I am able to get my For Loop to work clearing each cell one by one with a 1sec pause in between but I cant figure out how to use setValue to copy the value back into the cell. If I change the last line to source.setValue("test") then my script works, clearing each cell then replacing it with "test".
Here is my code:
function forceRefresh() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Fidelity Daily");
for (var i =6 ; i<=17;i++)
{
var source = sheet.getRange(i,3);
var values = source.getValues();
source.clear();
Utilities.sleep(1000);
source.setValue(values);
}
}
Values retrieved by getValues() is 2 dimensional array. In your script, values is [["value"]]. So for your script in your question, when you want to import values to a cell using setValue(), it is required to modify as follows.
From :
source.setValue(values);
To :
source.setValue(values[0][0]);
Note :
If you want to retrieve one value from one cell, you can use getValue(). In this case, value retrieved by getValue() can be used by source.setValue(value);.
References :
getValue()
getValues()
setValue()
I don't know whether this is an answer for you. If I misunderstand your question, I'm sorry.
I was able to solve my issue by adding the command SpreadsheetApp.flush() after the source.clear() command.
The answer for your second question.
Utilities.sleep only runs in the beginning of each function only one time. I hope this helps.
And unfortunately there is no such command like delay(); or something, in google script.
I've been handling a big set of Data with automated scripts in google drive.
These script constantly update some cells in a column through different sheets.
Now here is the problem: Reading and writing in google scripts is super long. So I tried to get the column via getValues, dump it in a an array, do my researches and my mods there and write the whole array back to the sheet.
However, this erases the formulas that might have been in the column (getValues would then return the result of the formula). Using getFormulas wouldnt give back the values. I could write and read formulas and values, but that would add operations when I'm trying to save time.
How come google doesn't provide a way to directly dump a String content of each cell? Any work around?
A.
Well, I could find any google specified answer, but I developed a merge function and it's actually fast enough, if anyone needs it :
function dumpColumn(sheet, index){
var range = sheet.getRange(1, index, sheet.getLastRow(), 1);
var arrV = range.getValues();
var arrF = range.getFormulas();
for(i = 0; i<arrF.length; i++){
if(arrF[i][0] != "")
arrV[i][0] =arrF[i][0];
}
return arrV;
}
I have a sheet that has some dates on it, like "25/02/2016", listed down a column. On the cells to the right of each date, there are some numeric values.
I need to copy these numeric values to a specific range on a different sheet. Ideally, it would work like a one-way sync, where I would write values on sheet1 and sheet2 would automatically be updated.
I've been looking through the Google Apps Script documentation, but I have no idea where to start. I do have some pseudo-code, just don't how to use it here.
function getDates() {
for (count = 0; count < sheet1.length; count++) {
if (hasDate) {
return(cell);
}
}
}
var numericValuesRows = sheet1.getDates().getRow();
var numericValuesRange = numericValuesRows.selectColumns(C-F);
Just making up method names and syntax. This bit is supposed to find out which rows have dates in them, and then select columns C to F in those rows. The next one is supposed to select the destination as all the cells from row3:columnC to row10:columnF, and copy the previously selected values to there.
var outputRange = sheet2.cellRange(C3-F10);
numericValuesRange.copyTo(outputRange);
I realise it's really crappy pseudo-code, but I'm hoping it at least helps in some way get across what I want to do. What would be the best way to do this?
Use the onEdit() trigger which will trigger when you edit a sheet. You can check the source of the edit to make sure its an edit to the cells you want in a particular sheet. Once that's done, its a matter of using getValue(), setValue() and getSheetByName(). Start here: simple triggers
I'm trying to develop an interactive spreadsheet that creates a narrative for a budget document. There will be a variety of options. Once the user selects an item, it will help them calculate the total. I want to setup option boxes that they fill in. For example, four cells will be allowed for input B1:B4. I am going to name each of the four cells (i.e. A, B, C, D). In the reference document I want to write various formulas. In some cases I might need "(A+B)*C" in another I might need "A * B * C" or "(A+B+C)/D" ... The spreadsheet would lookup the text formula, and I want to then convert it. So in the case of the lookup finding "(A+B)*C" I want it to convert it to =(indirect(A)+indirect(B))*indirect(C) which would then get the values from A (which is B1), B (which is B2) and so on.
Essentially, I would like to use or create something that is exactly the opposite of Excel's FORMULATEXT() function. I would prefer to do this in Google Sheets but I am willing to use Excel if I need to. I have a very basic understanding of both Google's scripting and VBA, and am willing to create if necessary, but not even sure how to tackle it.
Suggestions?
I found a way to do it in Google Apps Script:
function reCalc() {
var sheet = SpreadsheetApp.getActiveSheet();
var src = sheet.getRange("J26"); // The cell which holds the formula
var str = src.getValue();
str = str.replace('A', 'indirect("OPTA")').replace('B', 'indirect("OPTB")').replace('C', 'indirect("OPTC")').replace('D', 'indirect("OPTD")').replace('ENR', 'indirect("ENR")');
var cell = sheet.getRange("J30"); // The cell where I want the results to be
cell.setFormula(str); // Setting the formula.
}
Thank you to SpiderPig for giving me the idea!