Google Spreadsheet - Insert cut/copied cells/rows - google-apps-script

Google Spreadsheet doesn't have the functionality to "insert cut cells" like in Excel.
Let's say I selected A4 to B5, hit Ctrl+X.
Then I moved the selection to A2.
Now I want to "insert cut cells", probably by inserting blank cells and moving the dotted-range to A2.
Is there any way to do it using JavaScript on your own menu?
eg.
function insertCutOrCopiedCells(){
var SS = SpreadsheetApp.getActiveSpreadsheet();
var sheet = SS.getActiveSheet();
var cell = sheet.getActiveCell();
// How do you get the cells being cut/copied? (ie. A4 to B5)
// We can then insert empty cells (2 cols 2 rows) at the selection (A2)
// and move the cut cells (A4 to B5) there
}

To achieve the insert cut cells feature in google sheets you simply use drag and drop. First highlight the row(s) you want to move then mouse over the row number (you will see a hand icon); then, holding your left-click, and keeping your mouse over the row numbers, you will see a dark line where the insert will take place. Let go of your mouse button and voila.

You have both methods, check'em in the class Range.
moveTo(target)
Cut and paste (both format and values) from this range to the target range.
copyTo(destination)
Copies the data from a range of cells to another range of cells. Both the values and formatting are copied.
Edit:
To complete the function you'll have to use also:
insertRowsAfter(afterPosition, howMany) in class Spreadsheet
Inserts a number of rows after the given row position.
and
getActiveRange() with getRow() to check where's the selection at:
Returns the range of cells that is currently considered active. This generally means the range that a user has selected in the active sheet, but in a custom function it refers to the cell being actively recalculated.
Since you don't have direct acess to the clipboard, you'll have to set up a sidebar, or a modelessDialog, which asks for a range to copy from, and it would paste into the selected area, or the other way around, paste the current selected area onto an inputed ROW.

I think , you can separate the function.
Copy : use getRange with getValue:
link => https://developers.google.com/apps-script/reference/spreadsheet/sheet
Delete data : use the getRange with setValue = blank
example :
var Spreadsheet=
SpreadsheetApp.openById(SPREADSHEET_ID).getSheetByName("number1");
var value = Spreadsheet.getRange(1,0).getValue(); // copy
Spreadsheet.getRange(1,1).setValues(value); // insert
Spreadsheet.getRange(1,0).setValues("");
You can use the metho copyTo
example :
var sheet = SpreadsheetApp.getActiveSheet();
sheet.getRange("A:E").copyTo(sheet.getRange("F1"), {contentsOnly:true});
}

Related

Google sheets Script include formula when adding new rows

I want to include a script on my sheet to include N/A with a formula to certain cells (Example: I6:K6 & N6) but only to include this if C6 contains "A or B" if C6 contains "C" then not to include the formula.
I wouldn't be able to use an array formula within a cell as the value where N/A would go may need to be manually overridden.
I have tried a few different ways and I can only get it to work to check if the current cell you are in contains the text and it will trigger to apply N/A in the other cells. However it will only do the N/A if you are still on the active cell, if you move off that cell it will stop the trigger.
if (values[i][0].match("A") != null) {
var nextCell = iis.offset(0, 6);
if (nextCell.getValue() === '');
nextCell.setValue("N/A");
}
}````
I have also tried another way but it will override all of the existing data I already have within my sheet which is not what I want.
````var lastRow = ii.getLastRow();
ii.getRange("I3").setFormula("=IF(REGEXMATCH(C3,\"A|B\"),\"N/A\",\"\")");
var fillDownRange = ii.getRange(3, 9, (lastRow - 1));
ii.getRange("I3").copyTo(fillDownRange);````
You could try an approach like I exposed in this answer, if you're able to add a couple of hidden columns (one before your current I and K, and other before N). This way you'll be able to use an ARRAYFORMULA they will expand to adjacent cells when you specify it. Let me know!

Trying to run macro to add formula onto the end of an entire google sheet column

I have recorded a macro that adds formula onto the end of a cell
function addpricemultiplier() {
var spreadsheet = SpreadsheetApp.getActive();
spreadsheet.getRange('H5:1000').activate();
spreadsheet.getCurrentCell().setFormula('=0.3*GOOGLEFINANCE("CURRENCY:USDAUD")');
spreadsheet.getRange('H5:1000').activate();
};
The problems I have are that it will only change if the value is =0.3, I am unsure how to change it so that it will add the formula on no matter what, and of less importance I have made the macro from H5 onto 1000, I am unsure if there is a way to make it just go on as long as the sheet is there instead of pre determining where it will go to.

How do I insert new text (with a line break) to a cell without replacing existing cell value using Google Apps Script?

I need help inserting new text into a cell with a line break using Google Apps Script.
I created a form for my Marketing Calendar so all inputs will filter into respective tabs (Months) and into respective cells (Days). Thing is, I don't know how to add cell value/text that doesn't delete the previous data present in the cell.
What would be a proper code to add a line break + new text to a cell without removing existing data on it?
const sheet = SpreadsheetApp.getActiveSheet();
const range = sheet.getRange('A1');
let value = range.getValue();
value += '\nNew Text';
range.setValue(value);

Increment Cell value by one by clicking

I want to write a formula in Google sheets so that I can attach to a button to make the value in a cell increase by 1 each time you click it.
Here is the best I could find on this topic.
I attempted to do this, but to no avail. I am using Windows 7 and Chrome.
First create a script to increment the cell value. For instance, to increment cell "A1" by one the following script would work:
function increment() {
SpreadsheetApp.getActiveSheet().getRange('A1').setValue(SpreadsheetApp.getActiveSheet().getRange('A1').getValue() + 1);
}
Save this script and open your spreadsheet and follow these steps:
Go to "Insert" > "Drawing" and draw a button using shapes and text.
Position your button accordingly, and right-click on it to display an
arrow on the side of it.
In the drop-down menu, select "Assign Script." and type the name of
your function. (In this case "increment")
The first time when you click on it, it'll ask for permission but
should work subsequently.
Update 2020
Motivation:
I would like to provide a different approach based on Marlon's comment:
what if we want to create multiple buttons to different rows and each
button modifies its own row? Is there a way not create this manually?
Solution:
You can use onSelectionChange to capture single click events and mimic the behaviour of a button.
The following script will increment the value of a cell in column A upon clicking on a cell in column B of the same row.
It also creates a "button" text on the fly but this is optional and up to the user.
To use this solution, simply copy & paste it to the script editor and save the changes. After that, it will automatically be used upon single click events. In this solution I used Sheet1 as the sheet name.
function onSelectionChange(e) {
const as = e.source.getActiveSheet();
const col = e.range.getColumn();
const row = e.range.getRow();
if (as.getName() == 'Sheet1' && col == 2){
const range = as.getRange(row,1);
range.setValue(range.getValue()+1);
e.range.setValue('Button');
}
}
Demonstration:
Restrictions:
While this approach works pretty well, there are two drawbacks:
There is a small delay between the clicks and the updates.
The trigger is activated when selecting a cell. If a cell is already selected, if you click it again the function won't trigger. You have to remove the current selection and then select it again.
Assign the following to a drawing inside your sheet and it will increase the value of the currently selected cell by 1:
function plusOne() {
var cell = SpreadsheetApp.getActiveSheet().getActiveCell();
var value = cell.getValue() * 1;
cell.setValue(value+1);
}

Get cell note value

Is there a way to get cell note value and display the all the note in the cell next to it?
I have a column C where some of the cells contain note. I would like to get these note value and write each cell note in the cell next to it in column D.
For example: if cell C4 has a note "No entry", I want to display "No entry" in D4.
Thanks.
Alex
I needed to do this today and found your question but no answer. This is what I finally came up with. You can do this by setting up a function in the script editor named getNote.
function getNote(cell)
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var range = ss.getRange(cell)
return range.getNote();
}
then in your spreadsheet enter:
=getNote("C4")
to get the note from cell C4. If you want it to be a reference that changes when you move around the cells, then reference it this way:
=getNote(Address(row(C4), column(C4)))
Word of caution: Notes don't trigger a refresh, so if a note is edited the cell does not pick up the new value right away