I am trying to write a script that will look for a new entry from a Google Form into a Google Sheet and move data from specific cells to a separate spreadsheet. I have the following script and am having trouble with the rowValues variable. Below is the script as it is currently written.
function onEdit(){
var ss = SpreadsheetApp.getActive();
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("CopyFromSheet");
var rowId = sheet.getActiveRange().getRowIndex();
var rowValues (rowId);
Logger.log(rowValues);
var destValues = [];
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][1]);// copy data from col B to col B
destValues.push(rowValues[0][2]);// copy data from col C to col C
destValues.push(rowValues[0][5]);// copy data from col F to col D
destValues.push(rowValues[0][6]);// copy data from col G to col E
destValues.push(rowValues[0][7]);// copy data from col H to col F
destValues.push(rowValues[0][8]);// copy data from col I to col G
destValues.push(rowValues[0][21]);// copy data from col V to col H
destValues.push(rowValues[0][23]);// copy data from col X to col I
var dest=SpreadsheetApp.openById('SheetID#').getSheetByName('CopyToSheet');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}
function onFormSubmit(e){
var destValues = [];
destValues.push(e.values[0]);
destValues.push(e.values[1]);
destValues.push(e.values[2]);
destValues.push(e.values[5]);
destValues.push(e.values[6]);
destValues.push(e.values[7]);
destValues.push(e.values[8]);
destValues.push(e.values[21]);
destValues.push(e.values[23]);
var dest=SpreadsheetApp.openById('SheetID#').getSheetByName('CopyToSheet');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}
Related
I would like to have it that when a row is edited that it only copies the name phone and adds a timestamp of the edit on another tab in Google Sheets.
function onEdit(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Main');
var date = new Date();
var rowIdx = sheet.getActiveRange().getRowIndex();
var rowValues = sheet.getRange(rowIdx,1,1,sheet.getLastRow()).getValues();
Logger.log(rowValues);
var destValues = [];
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(rowValues[0][1]);// copy data from col B to col C
destValues.push(rowValues[0][3]);// copy data from col D to col D
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
}
The above is working but i cannot figure out how to get the timestamp to show.
The outcome should display the roadname, phone and then C:C should have timestamp of change made.
I have updated the below sheet to show expected outcome
https://docs.google.com/spreadsheets/d/1V3Ne9L0kB97OkFKwlidx40yN__sx78aRik_Ujiej2Wc/edit#gid=1523701088
How I want it to look.
Result Image
Data source
Source Data
Your written explanation is inconsistent with your sample output which is inconsistent with your script. destValues.push()-you are pushing 4 (four) values onto the array (including "Phone Number", "Date Contacted" and "Clan Name") but your expected outcome only has three columns (excluding "Date Contacted" and "Phone number"), but the "Phone number" is in the "Clan Name" column.
REPLACE
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(rowValues[0][1]);// copy data from col B to col C
destValues.push(rowValues[0][3]);// copy data from col D to col D
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
WITH
var date = new Date()
destValues.push(rowValues[0][0]);// copy data from col A to col A
destValues.push(rowValues[0][2]);// copy data from col C to col B
destValues.push(date);// copy data from col B to col C
var dest = SpreadsheetApp.getActive().getSheetByName('Updates');
dest.getRange(dest.getLastRow()+1,1,1,destValues.length).setValues([destValues]);
i want to connect ranges sheetA to SheetB.
sheet A ==================================
A B C D E F
1 x x x
2
sheet B ==================================
A B C
1 =sheetA!(A1) =sheetA!(B1) =sheetA!(C1)
2
i already used copyto, only con copy original link.
sheetA.getrange(1,3,1,3).copyto(sheetB.getrange(1,1,1,3))
Set sheetB!A1 to =sheetA!A1
Then, use autofill to fill up rest of the range
Sample script:
var sheet = SpreadsheetApp.getSheetByName("sheetB");
var sourceRange = sheet.getRange("A1");
sourceRange.setValue("=sheetA!A1")
var destination = sheet.getRange("A1:C20");
sourceRange.autoFill(destination, SpreadsheetApp.AutoFillSeries.DEFAULT_SERIES);
I'm trying to make a google sheet script that adds a row based on cell value, basically if I have in the Quantity (Column D) 7x laptops, I want the script to add 6 additional rows below if Column H is marked as "Yes" through data validation.
What I was able to find and to do is only duplicate that row but is without data validation and I would prefer to add the data validation and possible make each quantity split to 1 (instead of 7) after the duplication.
`function autoDup() {
var sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
var data = sheet.getDataRange().getValues();
var newData = [];
for(var n in data){
newData.push(data[n]);
if(!Number(data[n][3])){continue};// if column 3 is not a number then do nothing
for(var c=1 ; c < Number(data[n][3]) ; c++){ // start from 1 instead of 0 because we have already 1 copy
newData.push(data[n]);//store values
}
}
sheet.getRange(1,1,newData.length,newData[0].length).setValues(newData).sort({column: 1, ascending: false});// write new data to sheet, overwriting old data
}`
Hope someone is able to help me.
Thank you,
Column D contains a qty and goods description. If Column H = "Yes", you want to insert a number of rows below Col D equal to the qty minus one. If Column H <> "Yes, then take no action.
Sample data - Before
Sample data - After
function so5925663201() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheetname = "59256632";
var sheet = ss.getSheetByName(sheetname);
var row = 7;
// get value of Column H
var colHValue = sheet.getRange(row,8).getValue();
if (colHValue === "Yes"){
//Logger.log("DEBUG: Col H = yes. do something")
// get value of Column D
var Value = sheet.getRange(row,4).getValue();
var searchterm = "x";
var indexOfFirst = Value.indexOf(searchterm);
//Logger.log("DEBUG: the first instance of 'x' is "+indexOfFirst);
// get the quantity and convert from a string to a number
var qty = Value.substring(0, indexOfFirst);
var qtynum = +qty;
// var newtype = typeof qtynum; // DEBUG
//Logger.log("DEBUG: the quantity is "+qtynum+", new type = "+newtype)
// This inserts rows after
sheet.insertRowsAfter(row, qtynum-1);
}
else{
//Logger.log("DEBUG: col H <> Yes. do nothing");
}
}
I would like to create a Google Spreadsheets document that maintains:
Sheet 1 (All Entries): Unfiltered
Sheet 2 (1st Level Filter): Copies rows from Sheet 1 that Column B >/= 5000 OR Column C >/= 50000
Sheet 3 (2nd Level Filter): Copies rows from Sheet 1 that Column B >/= 5000 AND Column C >/= 50000
I created a sample doc for reference that's editable here.
Fairly limited programming/script experience.
In case you were looking for a script. This is what it'll look like although you'll have to do the mapping yourself. I haven't opened your doc but this will make it work if you need a script to do your bidding.
function sheetMaintain()
{
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1");
var s2 = ss.getSheetByName("Sheet2");
var s3 = ss.getSheetByName("Sheet3");
var r1 = s1.getDataRange();
var r2 = s2.getDataRange();
var r3 = s3.getDataRange();
var data1 = r1.getValues();
for (var i=0; i<r1.getLastRow(); i++)
{
if (data1[i][1]>=5000 || data[i][2]>=50000)
{
var colB = data[i][1];
var colC = data[i][2];
var lr2 = r2.getLastRow();
s2.getRange(lr2+1, 1).setValue(colB); //Mapping Col B value to Col A of Sheet 2
s2.getRange(lr2+1, 2).setValue(colC); //Mapping Col C value to Col B of Sheet 2
}
if (data1[i][1]>=5000 && data[i][2]>=50000)
{
var colB = data[i][1];
var colC = data[i][2];
var lr3 = r3.getLastRow();
s3.getRange(lr3+1, 1).setValue(colB); //Mapping Col B value to Col A of Sheet 3
s3.getRange(lr3+1, 2).setValue(colC); //Mapping Col C value to Col B of Sheet 3
}
}
}
if you'd like to not have any duplicates (from running the function time and again) in Sheet2 or Sheet3 I would also suggest you look into using the clear() function and set your Sheet2 and Sheet3 to clear before you parse through Sheet1 again.
I have added a formula to both sheets to achieve something like you may want
i.e. the first one below is the OR and the second is the AND
=filter('Sheet 1 - All Entries'!A:C,(('Sheet 1 - All Entries'!B:B>=5000) + ( 'Sheet 1 - All Entries'!C:C >=5000)))
=filter('Sheet 1 - All Entries'!A:C,(('Sheet 1 - All Entries'!B:B>=5000) * ( 'Sheet 1 - All Entries'!C:C >=5000)))
PS... I am not sure if a formula is sufficient for you or if you wanted this done through script
Is it possible to delete rows on other sheets based on 2 values? Say I have 3 sheets. In the main sheet (sheet 1), there will be 2 columns: Branch and Manager same with the remaining sheets.
SAMPLE SPREADSHEET HERE.
Example data:
SHEET 1: (main sheet)
--- BRANCH --- MANAGER ---
California Tom Chang
Brooklyn Jon Sieg
New York Raq Craig
SHEET 2:
--- BRANCH --- MANAGER ---
California Jane Cali
California Tom Chang
San Francisco James Chao
SHEET 2:
--- BRANCH --- MANAGER ---
California Jane Cali
California Tom Chang
New York Daniel Trevor
What should happen is that:
Branch column values should NOT duplicate in all sheets. So what we need to do is delete the row on sheet 2 and 3 if branch column is equal with the main sheet (sheet1) IF AND ONLY IF the manager is not the same/equal. So in my given data above, Branch California and Manager Tom Chang exists in all sheets therefore it should not be touched. But California branch was repeated in the remaining 2 sheets with a different Manager. Therefore, row California ---- Jane Cali should be deleted on Sheets 2 and 3.
Came upon a script borrowed from this post but can't seem to work. Here:
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainsheet = ss.getSheetByName("Sheet3").getDataRange().getValues();
var sheet2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var sheet3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate mainsheet and check in sheet2 & sheet3 if duplicate values exist
var nsheet2 = [];
var nsheet3 = [];
var mainsheetCol1 = [];// data in column1 of main sheet
var mainsheetCol2 = [];// data in column2 of main sheet
for(var n in mainsheet){
mainsheetCol1.push(mainsheet[n][0]); //column1
mainsheetCol2.push(mainsheet[n][3]); //column2
}
for(var n in sheet2){ // iterate sheet2 and test col 1 vs col 1 and co2 1 vs co2 1in sheet2
var noDup1 = checkForDup(sheet2[n],mainsheetCol1,mainsheetCol2)
if(noDup1){nsheet2.push(noDup1)};// if not present in sheet3 then keep
}
for(var n in sheet3){ // iterate sheet3 and test col 1 vs col 1 and co2 1 vs co2 in sheet3
var noDup2 = checkForDup(sheet3[n],mainsheetCol1,mainsheetCol2)
if(noDup2){nsheet3.push(noDup2)};// if not present in sheet3 then keep
}
// view result
Logger.log(nsheet2);
Logger.log(nsheet3);
// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
ss.getSheetByName("Sheet3").getDataRange().clear();
ss.getSheetByName("Sheet2").getRange(1,1,nsheet2.length,nsheet2[0].length).setValues(nsheet2);
ss.getSheetByName("Sheet3").getRange(1,1,nsheet3.length,nsheet3[0].length).setValues(nsheet3);
}
//Here can't seem to make it work to check if column 2 is not equal to the other sheets
//item is sheet2[n]
// s is mainsheetCol1
// s2 is mainsheetCol2
function checkForDup(item,s,s2){
Logger.log(s+' = '+item[0]+' ?')
Logger.log(s2+' = '+item[1]+' ?')
if((s.indexOf(item[0])>-1) && (s2.indexOf(item[1])>-1))){
return null;
}
return item;
}
Hoping someone could help/guide me. Thank you!
Try this:
function removeDuplicate(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var mainsheet = ss.getSheetByName("Sheet1");
var sheet2 = ss.getSheetByName("Sheet2");
var sheet3 = ss.getSheetByName("Sheet3");
var masterData = mainsheet.getDataRange().getValues();
var sheetsToCheck = [sheet2,sheet3];
for(var i in sheetsToCheck){
var valuesToCheck = sheetsToCheck[i].getDataRange().getValues();
for(var j=0;j<valuesToCheck.length;j++){
for(var k in masterData){
if(masterData[k][0] == valuesToCheck[j][0] && !(masterData[k][1] == valuesToCheck[j][1])){
sheetsToCheck[i].deleteRow(j+1);
}
}
}
}
}
Code given at Post is not working. I have modified it little bit to make it work.
Here it is,
function removeDupsInOtherSheets() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var s1 = ss.getSheetByName("Sheet1").getDataRange().getValues();
var s2 = ss.getSheetByName("Sheet2").getDataRange().getValues();
var s3 = ss.getSheetByName("Sheet3").getDataRange().getValues();
// iterate s3 and check in s1 & s2 if duplicate values exist
var nS1 = [];
var nS2 = [];
var s3Col1 = [];// data in column1 of sheet3
for(var n=0; n<s3.length; ++n){
s3Col1.push(s3[n][0]);
}
for(var n=0; n<s1.length; ++n){ // iterate sheet1 and test col 1 vs col 1 in sheet3
var noDup1 = checkForDup(s1[n],s3Col1)
if(noDup1){nS1.push(noDup1)};// if not present in sheet3 then keep
}
for(var n=0; n<s2.length; ++n){ // iterate sheet2 and test col 1 vs col 1 in sheet3
var noDup2 = checkForDup(s2[n],s3Col1)
if(noDup2){nS2.push(noDup2)};// if not present in sheet3 then keep
}
Logger.log(nS1);// view result
Logger.log(nS2);
ss.getSheetByName("Sheet1").getDataRange().clear();// clear and update sheets
ss.getSheetByName("Sheet2").getDataRange().clear();
var nS1Length = nS1.length;
var nS2Length = nS2.length;
// This is the change needed in code you have copied from,
**if(nS1 != undefined){
data = []
data[0] = nS1;
var range = ss.getSheetByName("Sheet1").getRange(1,1);
range.setValues(data);
}
if(nS2 != undefined){
data = []
data[0] = nS1;
var range = ss.getSheetByName("Sheet2").getRange(1,1);
range.setValues(data);
}**
}
function checkForDup(item,s){
Logger.log(s+' = '+item[0]+' ?')
if(s.indexOf(item[0])>-1){
return null;
}
return item;
}