Apps Script Google Sheets Formatting - google-apps-script

I have a script that creates a new Google Sheet file and names it based on a variable in a different sheet. After the sheet is created I am trying to copy all data from a template sheet, including merged cells and formatting. The scrip currently works in all aspects except the formatting. I am having a very hard time to get it to work. I would even be fine with just duplicating the sheet and having it rename based on my previous criteria.
function createNewSheet(e) {
const sh = e.range.getSheet();
var name = sh.getRange(e.range.rowStart, 1).getValue();
var crNew = SpreadsheetApp.create("Job Traveler-" + name);
var ssNew = SpreadsheetApp.openByUrl(crNew.getUrl()).getId();
importRange("1X4iOzc_shcOR8UM7OEM4F1-eR62YZmwKWyyEuu59Pf4","Job!A1:D", ssNew,"Sheet1!A1");
}
function importRange(sourceId,sourceRange,destinationID,destinationRangeStart) {
var sourceSS = SpreadsheetApp.openById(sourceId);
var sourceRng = sourceSS.getRange(sourceRange);
var sourceVals = sourceRng.getValues();
var destinationSS = SpreadsheetApp.openById(destinationID);
var destStartRange = destinationSS.getRange(destinationRangeStart);
var destSheet = destinationSS.getSheetByName(destStartRange.getSheet().getName());
var row = destStartRange.getRow();
var col = destStartRange.getColumn();
var rows = sourceVals.length;
var cols = sourceVals[0].length;
destSheet.getRange(row,col,rows,cols).setValues(sourceVals);
var merged = sourceRng.getMergedRanges();
var rowOffset = destStartRange.getRow()-sourceRng.getRow();
var columnOffset = destStartRange.getColumn()-sourceRng.getColumn();
for( var i=0; i<merged.length; i++ ) {
row = merged[i].getRow()+rowOffset;
col = merged[i].getColumn()+columnOffset;
rows = merged[i].getNumRows();
cols = merged[i].getNumColumns();
destSheet.getRange(row,col,rows,cols).merge();
}
}
Any advice would be much appreciated. I am no expert so I do apologize.
Best

Here is a snippet from one of my projects where I set the border and fill color
for (i = 0; i < numCount; i++) {
sheet.getRange(i + row2SetValues, col2SetValues).setValue(randNumbers[i]);
}
sheet.getRange(row2SetValues, col2SetValues, numCount, 1).setBorder(true, true, true, true, true, true, 'black', SpreadsheetApp.BorderStyle.SOLID_LIGHT);
sheet.getRange(row2SetValues, col2SetValues, numCount, 1).setBackground("#bdd4f9");
}

Related

Google script function works when it runs separately, but when I run all the functions together it doesn't work

I've got a question about my script. I've got a script where when running together only 2 out of 3 functions run, the other function does not run. The one function that does not work when running together is the "medewerkerLijst" function. When I run that function on it's own it works perfectly as it should, when running it together with the other 2 functions it does not work. I can't figure out why it's not working.
The functions "nieuwDossier" & "sorteerDossiers" do work when running together. Sorry if my code is a mess, I'm still learning how to code.
function Aanmaken(){
medewerkerLijst();
nieuwDossier();
sorteerDossiers();
}
function medewerkerLijst() {
var ss5 = SpreadsheetApp.getActiveSpreadsheet();
var copySheet2 = ss5.getSheetByName("-Dossier");
var pasteSheet2 = ss5.getSheetByName("-Medewerkers");
// get source range
var source2 = copySheet2.getRange(2,7,1,1);
// get destination range
var destination2 = pasteSheet2.getRange(pasteSheet.getLastRow()+1,1,1,1);
// copy values to destination range
source2.copyTo(destination2, {contentsOnly:true});
}
function nieuwDossier() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheet = source.getSheetByName('-Template');
sheet.copyTo(source).setName('Nieuw');
var sheet2 = source.getSheetByName('-Dossier');
var cell = sheet2.getRange("G2");
var value = cell.getValue();
var sheet3 = source.getSheetByName('Nieuw');
sheet3.setName(value);
sheet3.setTabColor(null);
}
function sorteerDossiers () {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetNameArray = [];
var sheets = ss.getSheets();
for (var i = 0; i < sheets.length; i++) {
sheetNameArray.push(sheets[i].getName());
}
sheetNameArray.sort();
for( var j = 0; j < sheets.length; j++ ) {
ss.setActiveSheet(ss.getSheetByName(sheetNameArray[j]));
ss.moveActiveSheet(j + 1);
}
}
This runs just fine
function main() {
var ss = SpreadsheetApp.getActive();
var sh1 = ss.getSheetByName("Sheet1");
var sh2 = ss.getSheetByName("Sheet2");
var rg1 = sh1.getRange(2, 7, 1, 1);
var rg2 = sh2.getRange(sh2.getLastRow() + 1, 1, 1, 1);
rg1.copyTo(rg2, { contentsOnly: true });
var sh3 = ss.getSheetByName('Sheet3');
var sh4 = sh3.copyTo(ss).setName('Sheet4');
var cell = sh1.getRange("G2");
var v1G2 = cell.getValue();
sh4.setName(v1G2);
sh4.setTabColor(null);
var shts = ss.getSheets().map(sh => sh.getName()).sort().forEach((n, i) => {
ss.setActiveSheet(ss.getSheetByName(n));
ss.moveActiveSheet(i + 1)
});
}

Copy Row from one Sheet to another sheet which is linked in the row

I have previously been able to copy rows from one Spreadsheet to another, however what I am trying to do now is copy a row to a specific spreadsheet that is linked on the same row and I can't seem to work it out.
I have around 500 rows of names with data (I cannot share a sheet as my firms Google does not allow for sharing externally but I have a screenshot of dummy data below). I used a formula to find the Unique names and then created a Google Sheet for each person and linked the Sheet back to the master data.
Master Data Sheet (tab is called Sheet1)
I am looking for a script that will work through the sheet, copying the rows to the link that is on the same row but I just can't figure it out - the following script does not work but I am at a loss so any help would be appreciated.
Apologies if I haven't explained myself very well!!
function copyTo(){
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("Sheet1");
var data = srcSheet.getDataRange().getValues();
for(var i = 1; i < data.length; i++) {
var row = data[i];
var link = row[3];
var id = row[4];
var complete = row[5];
var COMPLETE = "Complete"
if(link !== "" && complete !== "Complete"){
var srcRange = srcSheet.getRange("A" + i + ":C" + i);
var linkID = id.toString()
var tarSS = DriveApp.getFileById(linkID);
var tarSheet = tarSS.getSheetbyName("Sheet1");
var tarRow = tarSheet.getLastRow();
//tarSheet.insertRowAfter(tarRow);
var tarRange = tarSheet.getRange("A" + (tarRow+1) + ":C" + (tarRow+1));
srcRange.copyTo(tarRange);
srcSheet.getRange(i+1,6).setValue(COMPLETE)
}
}};
Try this:
function copyTo() {
var ss = SpreadsheetApp.getActive();
var sh = ss.getSheetByName("Sheet1");
var vs = sh.getDataRange().getValues();
for (var i = 1; i < vs.length; i++) {
var row = vs[i];
var link = row[3];
var id = row[4];
var complete = row[5];
var COMPLETE = "Complete"
if (link !== "" && complete !== "Complete") {
var rg = sh.getRange("A" + i + ":C" + i);
var linkID = id.toString()
var tarSS = SpreadsheetApp.openById(linkID);
var tarSheet = tarSS.getSheetbyName("Sheet1");
var tarRow = tarSheet.getLastRow();
var tarRange = tarSheet.getRange("A" + (tarRow + 1) + ":C" + (tarRow + 1));
rg.copyTo(tarRange);
sh.getRange(i + 1, 6).setValue(COMPLETE)
}
}
};
As much as I understand you question here is the script which will work for you exactly you want .
This will copy data from sheet you want and then create a new spreadsheet and paste into that sheet then link will show of this in same sheet from data copied
Let me know how this worked .
Code.gs
const sendDataToSheet = () => {
const ss = SpreadsheetApp.getActiveSpreadsheet()
const ws = ss.getSheetByName(''paste here your sheet name'')
const newSheet = SpreadsheetApp.create("give new sheet name here")
ws.copyTo(newSheet).setName("Copied Data")
const sheetURL = newSheet.getUrl()
ws.insertRowBefore(1)
ws.getRange(1,1).setValue(sheetURL)
}

Missing Headers row and Leaving space

I was working with a combine sheets app script which is working fine at the spot by mereging A bunch of 36 sheetsinto one masterenter code here
the only flaw of the script is that it is not showing a common header row in the top and also leaving a space after data end from first sheets and so on >>>
here the code can anyone help a bit here
function combineSheets() {
var sApp = SpreadsheetApp.getActiveSpreadsheet();
var s1= sApp.getSheetByName("A1");
var s2= sApp.getSheetByName("A2");
var s3= sApp.getSheetByName("A3");
var s4= sApp.getSheetByName("A4");
var s5= sApp.getSheetByName("A5");
var s6= sApp.getSheetByName("A6");
var s7= sApp.getSheetByName("A7");
var s8= sApp.getSheetByName("A8");
var s9= sApp.getSheetByName("A9");
var s10= sApp.getSheetByName("A10");
var s11= sApp.getSheetByName("A11");
var s12= sApp.getSheetByName("A12");
var s13= sApp.getSheetByName("A13");
var s14= sApp.getSheetByName("A14");
var s15= sApp.getSheetByName("A15");
var s16= sApp.getSheetByName("A16");
var s17= sApp.getSheetByName("A17");
var s18= sApp.getSheetByName("A18");
var s19= sApp.getSheetByName("A19");
var s20= sApp.getSheetByName("A20");
var s21= sApp.getSheetByName("A21");
var s22= sApp.getSheetByName("A22");
var s23= sApp.getSheetByName("A23");
var s24= sApp.getSheetByName("A24");
var s25= sApp.getSheetByName("A25");
var s26= sApp.getSheetByName("A26");
var s27= sApp.getSheetByName("A27");
var s28= sApp.getSheetByName("A28");
var s29= sApp.getSheetByName("A29");
var s30= sApp.getSheetByName("A30");
var s31= sApp.getSheetByName("A31");
var s32= sApp.getSheetByName("A32");
var s33= sApp.getSheetByName("A33");
var s34= sApp.getSheetByName("A34");
var s35= sApp.getSheetByName("A35");
var s36= sApp.getSheetByName("A36");
var s37= sApp.getSheetByName("Master");
// If Master doesn't exist you'll need to create it here.
var s1values = s1.getRange(2,1,s1.getLastRow(),27).getValues();
var s2values = s2.getRange(2,1,s2.getLastRow(),27).getValues();
var s3values = s3.getRange(2,1,s3.getLastRow(),27).getValues();
var s4values = s4.getRange(2,1,s4.getLastRow(),27).getValues();
var s5values = s5.getRange(2,1,s5.getLastRow(),27).getValues();
var s6values = s6.getRange(2,1,s6.getLastRow(),27).getValues();
var s7values = s7.getRange(2,1,s7.getLastRow(),27).getValues();
var s8values = s8.getRange(2,1,s8.getLastRow(),27).getValues();
var s9values = s9.getRange(2,1,s9.getLastRow(),27).getValues();
var s10values = s10.getRange(2,1,s10.getLastRow(),27).getValues();
var s11values = s11.getRange(2,1,s11.getLastRow(),27).getValues();
var s12values = s12.getRange(2,1,s12.getLastRow(),27).getValues();
var s13values = s13.getRange(2,1,s13.getLastRow(),27).getValues();
var s14values = s14.getRange(2,1,s14.getLastRow(),27).getValues();
var s15values = s15.getRange(2,1,s15.getLastRow(),27).getValues();
var s16values = s16.getRange(2,1,s16.getLastRow(),27).getValues();
var s17values = s17.getRange(2,1,s17.getLastRow(),27).getValues();
var s18values = s18.getRange(2,1,s18.getLastRow(),27).getValues();
var s19values = s19.getRange(2,1,s19.getLastRow(),27).getValues();
var s20values = s20.getRange(2,1,s20.getLastRow(),27).getValues();
var s21values = s21.getRange(2,1,s21.getLastRow(),27).getValues();
var s22values = s22.getRange(2,1,s22.getLastRow(),27).getValues();
var s23values = s23.getRange(2,1,s23.getLastRow(),27).getValues();
var s24values = s24.getRange(2,1,s24.getLastRow(),27).getValues();
var s25values = s25.getRange(2,1,s25.getLastRow(),27).getValues();
var s26values = s26.getRange(2,1,s26.getLastRow(),27).getValues();
var s27values = s27.getRange(2,1,s27.getLastRow(),27).getValues();
var s28values = s28.getRange(2,1,s28.getLastRow(),27).getValues();
var s29values = s29.getRange(2,1,s29.getLastRow(),27).getValues();
var s30values = s30.getRange(2,1,s30.getLastRow(),27).getValues();
var s31values = s31.getRange(2,1,s31.getLastRow(),27).getValues();
var s32values = s32.getRange(2,1,s32.getLastRow(),27).getValues();
var s33values = s33.getRange(2,1,s33.getLastRow(),27).getValues();
var s34values = s34.getRange(2,1,s34.getLastRow(),27).getValues();
var s35values = s35.getRange(2,1,s35.getLastRow(),27).getValues();
var s36values = s36.getRange(2,1,s36.getLastRow(),27).getValues();
// Now, we can put out all together and stuff it in Master
var s37values = [];
s37values = s1values.concat(s2values,s3values,s4values,s5values,s6values,s7values,s8values,s9values,s10values,s11values,s12values,s13values,s14values,s15values,s16values,s17values,s18values,s19values,s20values,s21values,s22values,s23values,s24values,s25values,s26values,s27values,s28values,s29values,s30values,s31values,s32values,s33values,s34values,s35values,s36values);
s37.getRange(1,1,s37values.length,27).setValues(s37values);
}
Modification points:
About not showing a common header row in the top, in your script, all values are retrieved from the 2nd row. I thought that this might be the reason for your issue. From a common header row, in this modification, the header row is retrieved from the 1st sheet.
About also leaving a space after data end from first sheets and so on, I thought that the reason of your issue is due to getRange(2,1,s1.getLastRow(),27). In this case, the values are retrieved from 2nd row to the last row + 1 rows. In this case, it required to be getRange(2,1,s1.getLastRow() - 1,27).
In your script, I thought that the sheet names from "A1" to "A36" can be created using a script.
When these points are reflected in your script, it becomes as follows.
Modified script:
function combineSheets() {
var sApp = SpreadsheetApp.getActiveSpreadsheet();
var values = [...Array(36)].map((_, i) => `A${i + 1}`).flatMap((s, i) => {
var sheet = sApp.getSheetByName(s);
var lastRow = sheet.getLastRow();
if (i == 0) {
return sheet.getRange(1, 1, lastRow, 27).getValues();
}
return lastRow > 1 ? sheet.getRange(2, 1, lastRow - 1, 27).getValues() : [];
});
sApp.getSheetByName("Master").getRange(1, 1, values.length, 27).setValues(values);
}
The headr row is retrieved from the 1st sheet.
Note:
In this modification, it supposes that the sheet names of "A1" to "A36" and "Master" are existing in your Spreadsheet. Please be careful about this.
From a common header row in the top, this script supposes that your all sheets have the same header row. Please be careful about this.
About Exception: The number of rows in the range must be at least 1. AT HERE................var values = [...Array(36)].map((_, i) => A${i + 1}).flatMap((s, i) => { var sheet = sApp.getSheetByName(s); return sheet.getRange(i == 0 ? 1 : 2, 1, sheet.getLastRow() - (i == 0 ? 0 : 1), 27).getValues(); , from your question, I had thought that your all sheets have the values. But from your reply, it seems that my understanding was not correct. So, I updated my proposed script. Could you please confirm it?
References:
getRange(row, column, numRows, numColumns)
map()

data transfert based on cell value on google sheet

I'm not good with coding and i tried to mix some codes that i found and made what i needed, but now i face a difficult problem for me, so I'm trying when to press the button that i created to run the script to send the data that have for example an 'X' in the last column. i successfully made the sending from one sheet to another but it send all the data for now i want to be sent only the chosen ones with an 'X'.
any help I'll be grateful.
this is my code
thank you
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var url = "https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit#gid=0";
var ss2 = SpreadsheetApp.openByUrl(url);
var pasteSheet = ss2.getSheetByName("Sheet2");
// get source range
var max = copySheet.getMaxRows().toString();
var range = copySheet.getRange(2,1,max,4);
var dataValues = range.getValues();
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][5] === 'X')
{
copySheet.appendRow([dataValues[i][0],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4]]);
}
}
for(var i = 1; i < dataValues.length; i++)
{
if(dataValues[i][5] === 'X')
{
var clearRow = i+1;
dataSheet.getRange('A' + clearRow + ':F' + clearRow).clear();
}
}
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,max,1);
// clear source values
Browser.msgBox('Commande Confirmer');
}
This is all that your function does
dataSheet is undefined so I assume it was csh
function lfunko() {
const ss = SpreadsheetApp.getActive();
const csh = ss.getSheetByName("Sheet1");
const ss2 = SpreadsheetApp.openByUrl("ssurl");
const psh = ss2.getSheetByName("Sheet2");
var vs = csh.getRange(2, 1, csh.getLastRow() - 1, csh.getLastColumn()).getValues();
vs.forEach((r,i) => {
if (vs[i][5] === 'X') {
csh.appendRow([vs[i][0], vs[i][1], vs[i][2], vs[i][3], vs[i][4]]);
csh.getRange(i + 1, 1, 1, 6).clear();
}
});
}

Google Scripts - Copy rows to a new sheet when column H has a specific colour (Yellow)

I've played around with this script but have still yet to get it to work.
I need a script that looks at the column F's background color in the sheet called Test.
If the cell background in Column F is yellow it will copy that entire row to a new sheet called New Data.
Please find attached the Google Spreadsheet link: https://docs.google.com/spreadsheets/d/19pMAmB94tOoV14MiSPyoPhty2dO5OudO4NupP8KhJA4/edit#gid=1703438513
Any help would be greatly appreciated!
function Extract(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Test');
var rows = sheet.getDataRange();
var numRows = rows.getNumRows();
var values = rows.getBackgrounds();
var yellowColour = '#ffff00';
var whiteColour = '#ffffff';
var newSheet = ss.getSheetByName('New Sheet');
for (var i = 0; i <= numRows - 1; i++) {
var row = values[i];
if (row[6] === whiteColour ) {
}
else {
newSheet.appendRow(row);
}
} // End of For Loop
};
Try this. It creates an array of rows where F is yellow. It clears the old data in 'New Data' and copies in the new array:
function Extract(){
var ss = SpreadsheetApp.getActive();
var sheet = ss.getSheetByName('Test');
var lr=sheet.getLastRow()
var data = sheet.getRange(2,1,lr,15).getValues();//get Test values
var rows = sheet.getRange(2,6,lr,1).getBackgrounds();//get F background colors
var yellowColour = '#ffff00';
var newSheet = ss.getSheetByName('New Data');
var lr1= newSheet.getLastRow()
var newData=[] //new array
for (var i = 0; i <= lr - 1; i++) {
if (rows[i] == yellowColour ) { //if F = yellow
newData.push(data[i]) // add to new array
}}
newSheet.getRange(2,1,lr1+1,15).clearContent()//clear old content excluding header row
newSheet.getRange(2,1,newData.length,15).setValues(newData)//set new comtent
};