What I am trying to do is transfer rows depending on the value in column p starting at row number 7. If cell in column P has a value of " Order" then copy that row from column B to Q to a completely separate already made spreadsheet. I have the script written in the target sheet.
Currently my script does loop through the row and will console.log the data I need... My issue is I have tried multiple things to then write the data to the correct range and can't figure it out.. I need to write the data to starting at row7 columnB... could use a little help..
function transferMonth() {
// SETTING UP THE LAST MONTH SHEET TO PULL NON SOLD DATA FROM
const lastmonthSheetss = SpreadsheetApp.openById("ID OF SPREADSHEET").getSheetByName("CDJR");
const lastSourceRow = lastmonthSheetss.getLastRow();
const sourceRange = lastmonthSheetss.getRange(7, 2, lastSourceRow, 15);
const sourceData = sourceRange.getValues();
// SETTING UP THE SHEET WHERE WE WANT TO TRANSFER LAST MONTHS DATA TO
const targetsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TestCopy");
const lastRow = targetsheet.getLastRow();
const lastCol = targetsheet.getLastColumn();
var sdata = [];
// KEY FOR ACCESSING THE DATA PULLED FROM LAST MONTHS SHEET
//console.log (sourceData[0]);
//console.log(sourceData[0][1]);
//console.log(sourceData[0][3]);
//console.log(sourceData[0][5])
//console.log(sourceData[0][14])
//console.log(sourceData[1][1]);
//console.log(sourceData[1][14]);
//SETTING UP PLACE TO STORE VALUES THAT NEED COPIED
for (i = 0; i < sourceData.length; i++) {
if (sourceData[i][14] === 'Order') {
sdata.push.apply(sdata, lastmonthSheetss.getRange(i + 7, 2, 1, 15).getValues());
sdata.push(i);
}
console.log(sdata)
}
targetsheet.getRange(7,2).setValues(sdata);
}
You can start with this.
Script:
function transferMonth() {
// SETTING UP THE LAST MONTH SHEET TO PULL NON SOLD DATA FROM
const lastmonthSheetss = SpreadsheetApp.openById("ID OF SPREADSHEET").getSheetByName("CDJR");
const lastSourceRow = lastmonthSheetss.getLastRow();
const sourceRange = lastmonthSheetss.getRange(7, 2, lastSourceRow, 15);
const sourceData = sourceRange.getValues();
// SETTING UP THE SHEET WHERE WE WANT TO TRANSFER LAST MONTHS DATA TO
const targetsheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("TestCopy");
const lastRow = targetsheet.getLastRow();
// use filter to only get the data with 'Order' as the 15th column
var sdata = sourceData.filter(x => x[14] === 'Order');
// always write starting at B7
targetsheet.getRange(7, 2, sdata.length, sdata[0].length).setValues(sdata);
}
Sample Data:
Initial target sheet:
Output:
Note:
Your main issues were the wrong usage of setValues and the pushed index on the array, aside from that, your code should still be workable.
I'm on GSuite Business and i have made a Google Apps Script for read the content on a GDrive folder and write on Column A the name of the file and on Column B the GDrive Link of the file.
I have about 8800 Files on this folder and i go over the 30 minutes execution time quota.
at this time i don't have idea to solve this problem..
I past here my script...
This script are useful for see the list of images of the product for sync with my ecommerce.
function ImageDatabaseUpdate() {
//read file on my drive foldeer -> 06 - usato fine serie
var foldername = '06 - Usato e Fine serie';
var folderlisting = 'Database GTLG ';
var folders = DriveApp.getFoldersByName(foldername)
var folder = folders.next();
var contents = folder.getFiles();
var contents = folder.getFiles();
//Set file id and sheet Immagini
var ss = SpreadsheetApp.openById("1ok2tfpLFRHE6I4ehR9lfsLwtFy1UfTdJqbi-NCqGVS0");
var sheet = ss.getSheetByName('immagini');
//Write heading on first sheet row
sheet.appendRow(['name','link']);
var file;
var name;
var link;
var row;
//start reading and writing
while(contents.hasNext()){
file = contents.next();
name = file.getName();
link = file.getUrl();
sheet.appendRow([name, link]);
}
}
Your code goes into an infinite loop because you need contents.next() to move to the next file. Since the folderIterator doesn't increment, while(contents.hasNext()) will continue forever.
I have the following google apps script. It functions to look in my PENDING-IP tab and check the status (column G). If the status is "Screened - Ready for Review," then it moves the entire row of data to the SCREENED - READY FOR REVIEW tab.
My problem is this - the data in columns L, M, and N of the PENDING tab is a checkbox, but when it pushes it to the SCREENED tab, it changes it to TRUE or FALSE. Is there a way to modify my script to push the data with validation to retain the checkboxes? Thank you!
function screened() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('PENDING-IP'); //source sheet
var testrange = sheet.getRange('G:G'); //range to check
var testvalue = (testrange.getValues());
var csh = ss.getSheetByName('SCREENED - READY FOR REVIEW'); //destination sheet
var data = [];
var j =[];
//Condition check in G:G; If true copy the same row to data array
for (i=0; i<testvalue.length;i++) {
if ( testvalue[i] == 'Screened - Ready for Review') {
data.push.apply(data,sheet.getRange(i+1,1,1,25).getValues());
//Copy matched ROW numbers to j
j.push(i);
}
}
//Copy data array to destination sheet
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
//Delete matched rows in the source sheet
for (i=0;i<j.length;i++){
var k = j[i]+1;
sheet.deleteRow(k);
//Alter j to account for deleted rows
if (!(i == j.length-1)) {
j[i+1] = j[i+1]-i-1;
}
}
}
Try this
I haven't actually tested this except in a simpler situation
function screened() {
var ss=SpreadsheetApp.getActive();
var sheet=ss.getSheetByName('PENDING-IP');
var testrange=sheet.getRange(1,7,sheet.getLastRow(),1);
var testvalue=testrange.getValues();
var valids=testrange.getDataValidations();
var csh = ss.getSheetByName('SCREENED - READY FOR REVIEW'); //destination sheet
var data = [];
var valid = [];
var j =[];
var d=0;
for (var i=0;i<testvalue.length;i++) {
if (testvalue[i][0] == 'Screened - Ready for Review') {
data.push(sheet.getRange(i+1-d,1,1,25).getValues());//I am not sure but I could see having to put [0] at the end here
valid.push(sheet.getRange(i+1-d,1,1,sh.getLastColumn()).getDataValidations());//I am not sure but I could see having to put [0] at the end here
sheet.deleteRow(i+1-d++);//this should delete matching rows
}
}
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(data);
csh.getRange(csh.getLastRow()+1,1,data.length,data[0].length).setValues(valid);
}
What I've found is that you can treat the validations the same way you treat the data by replacing getValues() with getDataValidations() and setValues() with setValidations();
I have 5 possible locations where a user can enter either: "Yes, No, or any URL". If a URL is entered, I need the value of that cell (the URL) to be entered into an equation on a corresponding cell in a separate sheet. Here is what I have:
//Locations of the cells where the user can enter the URL.
var graphic1_loc = 'A62';
var graphic2_loc = 'A63';
var graphic3_loc = 'A64';
var graphic4_loc = 'A65';
var graphic5_loc = 'A66';
//The corresponding locations on the "Briefing" sheet where the images would get inserted.
var graphic1_placement = 'B45';
var graphic2_placement = 'B46';
var graphic3_placement = 'B47';
var graphic4_placement = 'B48';
var graphic5_placement = 'B49';
//main_gen is the name of the sheet where the user enters the data. This just grabs the value
//the user entered and stores it in the corresponding variable.
var graphic1 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic1_loc).getValue();
var graphic2 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic2_loc).getValue();
var graphic3 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic3_loc).getValue();
var graphic4 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic4_loc).getValue();
var graphic5 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic5_loc).getValue();
var graphics_placements = ["B45", "B46", "B47", "B48", "B49"]; //These are the corresponding cells where the image would be placed on a separate sheet.
//If any graphic is a URL, insert that image into it's corresponding cell in the Briefing tab.
for (var num = 0; num < 5; num ++) {
var graphicformula = '=IMAGE("' + graphic[num] + '",1)';
if (graphic[num].length > 5) {
SpreadsheetApp.getActiveSheet().getRange('Briefing!'+graphic[num]_placement).setFormula(graphicformula);
}
}
What I am trying to get the If statement within the For Loop to say is...If the length of the value of graphic1 is > 5 (if it isn't yes (3) or no (2) then it is assumed it is a URL) then find the corresponding cell on the "Briefing" sheet to insert the formula. The current way I have it written is not correct, but I wanted to attempt to write something to give you an idea of what I am after. Thanks for any help you can provide!
You want to run SpreadsheetApp.getActiveSheet().getRange('Briefing!'+graphic[num]_placement).setFormula(graphicformula); when the value of graphic# is URL.
For example, when the URL is in the cell A63 in the sheet main_gen, you want to put the formula of =IMAGE(URL,1) to the cell B46 in the sheet Briefing
If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.
Modification points:
In your script, you are trying to use graphic[num] as a variable of graphic1, graphic2,,,. In this case, this cannot be used as the variable.
In your situation, the cells A62:A66 in the sheet main_gen are correspoinding to the cells B45:B49 in the sheet Briefing. I think that this can be used for modifying your script.
The values of the cells A62:A66 in the sheet main_gen can be retrieved by ss.getSheetByName("main_gen").getRange("A62:A66").getValues().
In order to confirm whether the value of cell is the URL, in this modification, I used test().
I think that when the destination sheet is declared out of the for loop, the process cost can be reduced.
When above points are reflected to your script, it becomes as follows.
Modified script:
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("main_gen").getRange("A62:A66").getValues();
var dstSheet = ss.getSheetByName("Briefing");
var graphics_placements = ["B45", "B46", "B47", "B48", "B49"];
for (var num = 0; num < source.length; num ++) {
if (/https?:\/\//.test(source[num][0])) {
var graphicformula = '=IMAGE("' + source[num][0] + '",1)';
dstSheet.getRange(graphics_placements[num]).setFormula(graphicformula);
}
}
References:
test()
getValues()
If I misunderstood your question and this was not the direction you want, I apologize.
I'm trying to code a script that imports specific columns for a spreadsheet to another spreadsheet. However, currently i'm writing/reading every column separately, which takes quite a while. I do this because I don't need all the columns from the original sheet into the new sheet. I was wondering if my current script could be changed to get to a script where you once read the necessary columns and write them all together? I tried to search for the same but the different order of the columns and the need for specific columns makes it hard.
I hope someone could help me get to the answer.
Thanks in advance,
Tim
function getPDdataupper() {
var sheet = SpreadsheetApp.getActive().getSheetByName('SPR');
var Num = Browser.inputBox("What URL are do you want to copy from?");
var ssraw = SpreadsheetApp.openById(Num);//put Address in here*****
var sheetraw = ssraw.getSheetByName("SPR");
sheet.getRange(10,11,(sheet.getLastRow())-9,3).clearContent();
sheet.getRange(10,1,(sheet.getLastRow())-9,3).clearContent();
//ADJUST ROW
var lastrow = sheet.getLastRow()-10;
var lastrowcopiedsheet = sheetraw.getLastRow()-10;
var diff = lastrowcopiedsheet - lastrow;
if(diff > 0){sheet.insertRowsAfter(11,diff);}
if(diff < 0){sheet.deleteRows(20,(diff*-1)); }
sheet.getRange(11,1,(sheet.getLastRow())-11,33).clearContent();
var values = sheetraw.getRange(1,1,5,1).getValues();
sheet.getRange(1,1,5,1).setValues(values);
var values = sheetraw.getRange(10,3,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,2,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,38,sheetraw.getLastRow()-9,2).getValues();
sheet.getRange(10,3,(sheet.getLastRow()-9),2).setValues(values);
var values = sheetraw.getRange(10,43,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,6,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,44,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,5,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,6,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,7,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,54,sheetraw.getLastRow()-9,2).getValues();
sheet.getRange(10,8,(sheet.getLastRow()-9),2).setValues(values);
var values = sheetraw.getRange(10,58,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,10,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,14,sheetraw.getLastRow()-9,19).getValues();
sheet.getRange(10,14,(sheet.getLastRow()-9),19).setValues(values);
var values = sheetraw.getRange(10,51,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,33,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,62,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,34,(sheet.getLastRow()-9),1).setValues(values);
var values = sheetraw.getRange(10,4,sheetraw.getLastRow()-9,1).getValues();
sheet.getRange(10,35,(sheet.getLastRow()-9),1).setValues(values);
I have sorted it myself now. For someone who is looking for the same thing, please see the code below. If anyone else thinks of something better please feel free to amend.
function getPDdataupper() {
var sheet = SpreadsheetApp.getActive().getSheetByName('SPR');
var folder= DriveApp.getFolderById('FolderID');
var files = folder.getFilesByType(MimeType.GOOGLE_SHEETS);
while (files.hasNext()) {
var ssraw = SpreadsheetApp.open(files.next());
//var ssraw = SpreadsheetApp.open(Num);
}
sheet.getRange(11,1,(sheet.getLastRow())-10,53).clearContent();
var sheetraw = ssraw.getSheetByName("SPR");
sheet.getRange(10,11,(sheet.getLastRow())-9,3).clearContent();
sheet.getRange(10,1,(sheet.getLastRow())-9,3).clearContent();
//ADJUST ROW
var lastrow = sheet.getLastRow()-10;
var lastrowcopiedsheet = sheetraw.getLastRow()-10;
var diff = lastrowcopiedsheet - lastrow;
if(diff > 0){sheet.insertRowsAfter(11,diff);}
if(diff < 0){sheet.deleteRows(20,(diff*-1)); }
//var values = sheetraw.getRange(10,("C10:C,AL10:AM,AQ10:AR,F10:F,BB10:BC,BF10:BF"),(sheetraw.getLastRow()-9),53).getValues();
//sheet.getRange(10,2,(sheet.getLastRow()-9),(sheet.getLastColumn()-1)).setValues(values);
var data = sheetraw.getDataRange().getValues();{
var target = new Array();
for(n=0;n<data.length;++n){
target.push( [[data[n][2]], [data[n][37]], [data[n][38]], [data[n][43]], [data[n][42]], [data[n][5]], [data[n][53]], [data[n][54]], [data[n][57]]] );
}
sheet.getRange(1,2,target.length,target[0].length).setValues(target);
}