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();
}
});
}
Related
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)
});
}
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)
}
I have two issues on my code, due to my luck of experience in coding, the code that I have is made to copy rows with a specific cell value, in my case it's 'commande confirmer', the first problem that I have is the first line of my values even if it has the specific value it's not copying but on other lines it's working perfectly.
And for the 2nd issue I want to get also when the data copied it transfer with it to the active sheet email, I found how to get it, but how to include it and being transferred I couldn't make it.
Here is my code
and thanks in advance.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Sheet1");
var url = "https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit?ouid=112360896426304156672&usp=sheets_home&ths=true";
var ss2 = SpreadsheetApp.openByUrl(url);
var pasteSheet = ss2.getSheetByName("Sheet2");
// get source range
var max = copySheet.getMaxRows().toString();
var email= Logger.log(Session.getActiveUser().getEmail());
var range = copySheet.getRange(2, 1, max, 10);
var dataValues = range.getValues();
for (i = 1; i < dataValues.length; i++) {
if (dataValues[i][9] === 'COMMANDE CONFIRMER') {
pasteSheet.appendRow([dataValues[i][0],
dataValues[i][email],
dataValues[i][1],
dataValues[i][2],
dataValues[i][3],
dataValues[i][4],
dataValues[i][5],
dataValues[i][6],
dataValues[i][7],
dataValues[i][8],
dataValues[i][9]]);
var clearRow = i + 2;
copySheet.getRange('A' + clearRow + ':J' + clearRow).clear();
}
}
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, max, 1);
// clear source values
Browser.msgBox('Commande Confirmer');
}
Try it this way:
function funko() {
var ss = SpreadsheetApp.getActive();
var csh = ss.getSheetByName("Sheet1");
var ss2 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/15nIAXcP0a14OvvBr5lww4tO23stQD4PEu0QDAcgaFyE/edit?ouid=112360896426304156672&usp=sheets_home&ths=true");
var psh = ss2.getSheetByName("Sheet2");
var email = Session.getActiveUser().getEmail();
var range = csh.getRange(2, 1, csh.getLastRow() - 1, 10);
var cvs = range.getValues();
let oA = [];
cvs.forEach((r, i) => {
if (r[9] == 'COMMANDE CONFIRMER') {
let t = r.slice();
t.splice(1, 0, email);
oA.push(t);
csh.getRange(i + 1, 1, 1, csh.getLastColumn()).clearContent();
}
});
psh.getRange(psh.getLastRow() + 1, 1, oA.length, oA[0].length).setValues(oA);
Browser.msgBox('Commande Confirmer');
}
With this script, I can import one spreadsheet into another spreadsheet in a tab.
function importA() {
var values1 = SpreadsheetApp.openById('xxxurl').
getSheetByName('xxxname').getRange('A1:DE300').getValues();
SpreadsheetApp.getActive().getSheetByName('TAB').
getRange(1,1,values1.length,values1[0].length).setValues(values1)
}
Now, I would like to import 3 spreadsheets one below the other (in the same tab), preserving any increase in the rows of each sheet at the same time.
How could I proceed?
You can try this code and see if it works for you:
function collectAll() {
createNew(); //this can be removed for instances that you would need to run the two functions separately.
SpreadsheetApp.flush();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ["TAB1","TAB2","TAB3"];
for( var i=0; i<sheets.length; i++ ) {
if (sheets[i] == "TAB1"){
var sh = ss.getSheetByName('TAB1');
var range1 = sh.getRange(1,1, sh.getLastRow(), sh.getLastColumn()).getValues();
Logger.log(range1);
}
else if (sheets[i] == "TAB2"){
var sh = ss.getSheetByName('TAB2');
var range2 = sh.getRange(1,1, sh.getLastRow(), sh.getLastColumn()).getValues();
Logger.log(range2);
}
else if (sheets[i] = "TAB3"){
var sh = ss.getSheetByName('TAB3');
var range3 = sh.getRange(1,1, sh.getLastRow(), sh.getLastColumn()).getValues();
Logger.log(range3);
}
}
var range = range1.concat(range2,range3);
Logger.log(range);
ss.getSheetByName('TAB').getRange(1,1, range.length, sh.getLastColumn()).setValues(range);
}
function createNew() { //This function creates the Tabs from 3 different sheets using Sheet ID
var sheets = ["ID1","ID2","ID3"]; //Spreadsheet ID goes here
for (i=0; i<sheets.length; i++ ) {
var ssh = SpreadsheetApp.openById(sheets[i]);
var values = ssh.getSheetValues(1,1, ssh.getLastRow(), ssh.getLastColumn());
var create = SpreadsheetApp.getActive().insertSheet('TAB'+(1+i));
create.getRange(1,1, ssh.getLastRow(), ssh.getLastColumn()).setValues(values);
Logger.log(create);
}
}
What this does is that it assigns all of the values of each tab to its own variable range1, range2 and range3 and then it concatenates all values in a single range, then sets the value to the sheet named "TAB".
I've used getSheetByName instead of openByID but you can get the idea.
function test() {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheets = ["Sheet1","Sheet2","Sheet3"];
for( var i=0; i<sheets.length; i++ ) {
importA(sheets[i]);
}
}
catch(err) {
console.log(err);
}
}
function importA(name) {
try {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sh1 = ss.getSheetByName("TAB");
sh1.clear() // if you want to erase old data in TAB
var sh2 = ss.getSheetByName(name);
var r1 = sh1.getDataRange();
var r2 = sh2.getDataRange();
// to prevent a blank line at the top
var i = r1.getNumRows() === 1 ? 0 : 1;
sh1.getRange(r1.getNumRows()+i,1,r2.getNumRows(),r2.getNumColumns()).setValues(r2.getValues());
}
catch(err) {
console.log(err);
}
}
Merge all spreadsheets sheets into one sheet
function mergeSpreadsheetsAllIntoOneSheet() {
const ss = SpreadsheetApp.getActive();
['id1', 'id2', 'id3'].forEach((id, i) => {
let sss = SpreadsheetApp.openById(id);
sss.getSheets().forEach(sh => {
let vs = sh.getDataRange().getValues();
let a = [...Array.from(new Array(vs[0].length).keys(), x => (x == 0)?sss.getName():sh.getName())];
vs.unshift(a);
let ash = ss.getSheets()[0];
if (vs && vs.length > 0) {
ash.getRange(ash.getLastRow() + 1, 1, vs.length, vs[0].length).setValues(vs);
}
});
});
}
I am trying to transfer a row (range A:AE) from sheet NSI to the next available row in Sheet1 if cell AE= 1.
My code doesn't seem to work, please could somebody help me?
Thank you!!
function myFunction() {
function copyRows() {
var sSheet = SpreadsheetApp.getActiveSpreadsheet();
var srcSheet = sSheet.getSheetByName("NSI");
var lastRow = srcSheet.getLastRow();
for (var i = 2; i <= lastRow; i++) {
var srcRange = srcSheet.getRange("A" + i + ":AE" + i);
var cell = srcSheet.getRange("AE" + i);
var val = cell.getValue();
//sets the target sheet depending on the exam in column AE
if (val == "1") {
var tarSheet = sSheet.getSheetByName("Sheet1");
}
//insets the row in the correct target worksheet
var tarRow = tarSheet.getLastRow()+1;
tarSheet.insertRows(tarRow);
var tarRange = tarSheet.getRange("A" + (tarRow) + ":AE" + (tarRow));
srcRange.copyTo(tarRange);
}
};
}
function copyRows() {
var sSheet = SpreadsheetApp.getActive();
var srcSheet = sSheet.getSheetByName("NSI");
const vs = srcSheet.getRange(2, 1, srcSheet.getLastRow() - 1, 31).getDisplayValues();
var tarSheet = sSheet.getSheetByName("Sheet1");
vs.forEach(r => {
if (r[30] == "1") {
tarSheet.appendRow(r);
}
});
}