Google Apps Script: copy ranges to new range use connect formula - google-apps-script

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

Related

Copy Row Contents but Only Certain Cells and add Timestamp

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

Moving specific cells from last row to new spreadsheet

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

script of Comparing 2 columns and copy value if match between two google sheets,

Can Anyone please help to share the google script of compare value between two sheets and return different value on 1st sheet if value match?
For example
Sheet1 (Before)
DESCRIPTION MEMBERCODE BOX 1 ABOX 2 B BOX 3 C BOX 4 DBOX 5 E
Sheet2
MEMBERCODE PRICEE $14A $10D $13C $12B $11
Sheet1 (After)
DESCRIPTION MEMBERCODE PRICE BOX 1 A $10BOX 2 B $11BOX 3 C $12 BOX 4 D $13BOX 5 E $14
Try this:
function compareAndCombine() {
var ss=SpreadsheetApp.getActive();
var sh1=ss.getSheetByName('Sheet1');
var v1=sh1.getRange(2,1,sh1.getLastRow()-1,sh1.getLastColumn()).getValues();
var sh2=ss.getSheetByName('Sheet2');
var v2=sh2.getRange(2,1,sh2.getLastRow()-1,sh2.getLastColumn()).getValues();
var v2A=v2.map(function(r){return r[0]});
v1.forEach(function(r,i){
var idx=v2A.indexOf(r[1]);
if(idx>-1) {
v1[i].splice(2,0,v2[idx][1]);
}else{
v1[i].splice(2,0,'');
}
});
sh1.getRange(2,1,v1.length,v1[0].length).setValues(v1);
}

Google Forms response in new sheet with notification

I've created a order capture form that captures the
1. location (dropdown),
2. delivery date (date)
3. Products (small text)
I have then added a formula to generate a new order no. everytime a new response is added.
=arrayformula( if( len(A2:A), "Order " & text(row(A2:A) - row(A2) + 1, "022"), iferror(1/0) ) )
I need a new sheet to be created with the tab name with the new order no. and the columns transposed along with the response inputs against them.
For example response form
Column B | Column C | Column D |Column E |Column F
Order no. | Location | Delivery Date | Product 1 | Product 2
Order 122 | XYZ | 2/16/2017 | 35kg | 45kg
New sheet generated named "Order 122"
Order no. | Order 122
Location | XYZ
Delivery Date | 2/16/2017
Product 1 | 35kg
Product 2 | 45kg
and a notification to be sent to an email.
Is this possible with google spreadsheets, if so, please do let me know how it can be done. Thank you in advance.
I am using the following to create a new sheet named after the order no. but it doesnt seem to be working based on this article.
function subTheForm(){
Logger.log('submit ran');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
var lastRow = sheet.getLastRow();
var colB_Data = sheet.getRange(lastRow, 2).getValue();
ss.insertSheet(colB_Data);
};
I get this error when I run the script
The sheet name cannot be empty. (line 9, file "Code")
Solved.
I used the following code similar as what I'd posted. For some reason I kept getting an error, cleared the responses, deleted and recreated the script and triggers and it worked.
function onSubmit(e){
Logger.log('submit ran');
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
//Get last row of data
var lastRow = sheet.getLastRow();
var colB_Data = sheet.getRange(lastRow, 2).getValue();
ss.insertSheet(colB_Data);
};

Copy row into new sheet(s) if two different cell values are within a defined range

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