Force Copy and Paste of the text only G-Sheet - google-apps-script

I created a document in google sheet, which uses conditional formatting. This document is shared with other people and each time they copy and paste from one cell to another, they also copy the conditional formatting of the cell of origin. Is it possible to allow the copy and paste of the text only for that document, without formatting?
I found the solution to run the script every night that reorders the conditional formatting. Other solutions to avoid this? Thank you

I don't know if this is a workable solution solution but I created conditional format rule for a sheet which governs the entire sheet. For column 1, I created some rules that control background color based upon the numeric values. For the entire rest of the sheet I set the conditional format background color to '#ffffff' which is my default color. So that when you copy from column 1 to any other column the conditional format rule that sets the background color to '#ffffff' governs the paste.
function customFormulaRule1() {
var sheet = SpreadsheetApp.getActiveSheet();
var rules=[];
var range=sheet.getRange(1,2,sheet.getMaxRows(),sheet.getMaxColumns());
var rule=SpreadsheetApp.newConditionalFormatRule()
.whenNumberBetween(1,10)
.setBackground('#ffffff')
.setRanges([range])
.build();
rules.push(rule);
range = sheet.getRange("A1:A10");
var rA=[{n:1,bg:"#b7e1cd"},{n:2,bg:"#fce8b2"},{n:3,bg:"#f4c7c3"}];
for(var i=0;i<rA.length;i++){
var rule = SpreadsheetApp.newConditionalFormatRule()
.whenNumberEqualTo(rA[i].n)
.setBackground(rA[i].bg)
.setRanges([range])
.build();
rules.push(rule);
}
sheet.setConditionalFormatRules(rules);
}
In the following image I show what happens when you copy six cells from column A to column B.

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!

How to apply the same conditional formatting rule to multiple columns in Google sheets using apps script?

I have multiple columns - G, L, Q, V, AA, AF - to which I want to apply a conditional format rule in Google sheets. Starting in row 2 - so that I don't include the header row - I want any given cell in the specified column to .setBackgroundColor ("orange") if any text or data is found to the right of that cell. For example, I want cell G2 to be orange if cell H2 has anything entered inside of it, and L17 to be orange if S17 has data, and AA5 to be orange if AD5 is not blank.
My experience with apps script is very primative. I can only successfully write very few lines of code, so my dilemma is past my ability. I understand it is possible to apply conditional formatting using sheets' built in conditional formatting tab, but it will not work for my project seeing as I am gathering data with a Google form, and with every response I receive from the form, the sheet creates a new line for the submission that retains none of the formatting from the rest of the sheet. My plan would be to add a form submission trigger to the code so that the conditional formatting in the columns updates regularly with the sheet.
I have been looking around for some time online and have not found a solution to my problem. Any help would be greatly appreciated! Thank you!
The following example shows the process of creating a new conditional format rule, applying it to the sheet. Then copying and pasting the format of a range to a target range. You can find the references for the methods used in the references section at the end of this answer.
Example:
function myFunction() {
// The range where the formatting will be applied.
var range = 'G2:G'
// Get the array containing the conditional formatting rules for the sheet
var spreadsheet = SpreadsheetApp.getActive();
var conditionalFormatRules = spreadsheet.getActiveSheet().getConditionalFormatRules();
// Build the new conditional formatting rule and push it to the array
conditionalFormatRules.push(SpreadsheetApp.newConditionalFormatRule()
.setRanges([spreadsheet.getRange(range)]) // The range to apply to
.whenFormulaSatisfied('=H2<>""') // The formula to check
.setBackground('orange') // The format to apply
.build());
// Set the conditional format rules for the sheet with the updated array
spreadsheet.getActiveSheet().setConditionalFormatRules(conditionalFormatRules);
// Paste the format of the range into the next desired range
spreadsheet.getRange(range).copyTo(spreadsheet.getRange('L2:L'), SpreadsheetApp.CopyPasteType.PASTE_FORMAT, false);
/* repeat the previous instructions for all your target ranges replacing "L2:L" with the target range */
};
References:
-getConditionalFormatRules()
-newConditionalFormatRule()
-setConditionalFormatRules()
-copyTo(destination, copyPasteType, transposed)

Script to set default font size and style in google sheets

I have a shared google sheet where people constantly paste new data in however the data is always in different styling and restyling it is very time consuming. I was wondering if it it's possible with a script that auto correct styling and size in a document.
If you are looking at resetting it to the default theme then you can use this script:
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRange(1, 1, ss.getMaxRows(), ss.getMaxColumns());
range.clearFormat();
}
This triggers every-time a user edits the sheet (including pasting some data) and clears all formats in the sheet. If this doesn't work for you in case you have specific formats for some cells, like data headers that you don't want to change, then you can customise the script using methods like setFontSize(size) etc.
Edit: Changed the answer for the specific spreadsheet.
Use this script
function onEdit(e) {
var ss = SpreadsheetApp.getActiveSheet();
var range = ss.getRangeList(['B29:B42', 'B27']);
range.setFontFamily("Arial")
.setFontSize(11)
.setFontColor("#000000")
.setBackground("#ffffff");
}
The following script will reset the formatting on the edited range. You can further validate the row/column/sheet before resetting the formatting (eg. to prevent header rows from getting affected) using the values of e.range or e.range.getSheet()
function onEdit(e) {
e.range.setFontColor() // Resets color
.setFontSize(10) // Sets size to 10 (has to be a number)
.setFontFamily() // Resets font family
.setFontLine() // Resets underline, strikethrough
.setFontWeight() // Resets bold
.setFontStyle() // Resets italics
}
Instead of onEdit, some other options include
making a timed trigger that routinely resets the formatting
running a script manually when you would like to reset formatting
Refer to Range class.

How to remove data validations?

I want to remove all data validations from a sheet, tried following but it does not get rid of them. Anyway to do this through apps script?
var ss = SpreadsheetApp.getActiveSpreadsheet();
var accountSheet = ss.getSheetByName("Account A");
accountSheet.getRange(1,1,274,61).clear();
accountSheet.getRange(1,1,274,61).setDataValidation(null)
That should do the job.
A more generic answer to remove all data validations from the whole sheet (remove from all cells in sheet):
mySheet.getRange(1,1, mySheet.getMaxRows(), mySheet.getMaxColumns()).setDataValidation(null);
You could in theory use this less verbose version (might be faster because it affects a smaller range of cells):
mySheet.getDataRange().setDataValidation(null);
However, there may be empty cells outside the DataRange that do have validations and those validations will not be removed (they are outside the scope of DataRange, won't be selected by DataRange). Hence, you should use the first version I mentioned for more robust code: it selects all the cells in the sheet and removes validations.

Google Spreadsheet - Insert cut/copied cells/rows

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});
}