Google Sheets: Script to Paste and Transpose to Next Empty Row - google-sheets

I am very familiar with Excel, but this is my first time using Google Sheets and I am not sure where to start. I need a script that selects a range of cells (C2:C6 to be specific), copies them, then pastes and transposes them into the next blank row in a worksheet. In other words, I'm trying to dump data from a little 'form' I've created into another worksheet that contains all of the data users are entering.
This is what I've tried so far:
function TempPaste() {
var spreadsheet = SpreadsheetApp.getActive();
var destSheet = ss.getSheetByName("Steps");
spreadsheet.getRange('\'Step Entry\'!C2:C6').activate();
spreadsheet.setActiveSheet(spreadsheet.getSheetByName('Steps'), true);
spreadsheet.getRange('\'Step Entry\'!C2:C6').copyTo(spreadsheet.getActiveRange(), SpreadsheetApp.CopyPasteType.PASTE_NORMAL, true);
};
I just recorded that as a macro. Of course, it just pastes the same data into cell A1 in the 'Steps' worksheet, when I need it to paste the data into column B, always in the first empty row.

Related

How to transfer comments when transferring data from one table to another in Google Sheet with the QUERY+IMPORTRANGE function

comments transferred
How are comments written in cells transferred when data is moved
From one Google Sheet table to another while using a function
QUERY+IMPORTRANGE
At the moment it's not possible to show notes or comments by using IMPORTRANGE
You can fill out a Feature Idea to add that functionality into Google Sheets on https://www.googlecloudcommunity.com/gc/Feature-Ideas/gh-p/workspace-ideas-group
As a workaround, if you copy and paste the cells, comments and notes will be copied over, but you can do it programmatically in Google Apps Script by using copyTo
// The code below copies the first 5 columns over to the 6th column.
var sheet = SpreadsheetApp.getActiveSheet();
var rangeToCopy = sheet.getRange(1, 1, sheet.getMaxRows(), 5);
rangeToCopy.copyTo(sheet.getRange(1, 6));

Is there a way to use FILTER with multiple Sheets containing a specific phrase in Google Sheets?

I will try to be as clear as possible. Here is the example piece: link
What I want to happen is that the Filter formula will search for any Sheet containing “Form Responses” and then display the results. You can see on the Current sheet how I’ve been doing, but this is more tedious and leads to issues of the first formula begins to overwrite the next one, etc. On the Wanted tab, I’ve laid out how I imagine it and put a note in A7. Any help offered is greatly appreciated!
You can get started with this script:
function getSheetResponses(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName("Wanted");
var getCellValue = sheet.getRange("A7").getValue(); //Gets the name of your designated sheet on "Wanted Sheet" cell "A7"
var getName = ss.getSheetByName(getCellValue);
var getNameValue = getName.getRange(2,5,getName.getLastRow(),1).getValues(); //Gets all the values of Column E on any defined sheet names based on getCellValue
var datalength = getNameValue.length;
Logger.log(datalength);
sheet.getRange(8,6,datalength,1).setValues(getNameValue); //Puts the data on Wanted Sheet Column F
}
What this does is it gets the sheet name on cell A7, and populates the data on Column F row 8 on the "Wanted" sheet like so:
Now, the data it populates on the "Wanted" sheet came from Form Responses 1 based on the sample piece you have provided:
If ever you would want to relocate which specific row or column the data would be pasted on "Wanted" Sheet. You can refer to this documentation on how to modify the rows and columns on sheet.getRange()
Reference:
https://developers.google.com/apps-script/reference/spreadsheet/sheet#getrangerow,-column,-numrows,-numcolumns

Google Sheets - Pushing a row to a second spreadsheet based on a value being entered

I'm new to this,
I have 2 google spreadsheets:
Spreadsheet A: The active sheet Containing multiple tabs with information to be Pushed to B.
Spreadsheet B: A spreadsheet with a single tab. The same headers and structure as spreadsheet A.
Based on the user selecting the answer "Yes" in the first column of any of the 1 tabs in Spreadsheet A, I would like that entire row to move over to Spreadsheet B.
I have modified a script that works on a single spreadsheet (ie moving rows from tab to tab) to attempt to get it to work between spreadsheets:
function onEdit(event) {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var tss = SpreadsheetApp.openById('B').getSheetByName('Sheet 1');
var s = event.source.getActiveSheet();
var r = event.source.getActiveRange();
if(r.getColumn() == 1 && r.getValue() == "Yes") {
var row = r.getRow();
var numColumns = s.getLastColumn();
var target = tss.getRange(targetSheet.getLastRow() + 1, 1);
s.getRange(row, 1, 1, numColumns).moveTo(target);
}
}
Probably needless to say, this yields no result. Having searched through a number of posts and forums I only see individuals posting about how to move rows between tabs but not between entirely separate spreadsheets. Is it even possible to do this? If so, what am I doing wrong in the script?
Thank you so much for anyone who takes the time to assist.
Anthony
Following a dialogue with the OP in the comments section, it was indicated that the original spreadsheet did not to be kept secret.
Consequently, the desired functionality can be provided by using a combination of IMPORTRANGE() and QUERY() in the spreadsheet with no need to use Google App Script. For instance,
=QUERY(IMPORTRANGE(url,range),"select A where B matches 'Yes'") or similar
This imports data from a second spreadsheet and then the QUERY() function acts as a way of filtering the imported range by certain criteria.
Once the imported range is authorised, the editors of the spreadsheet can access it by, e.g. removing or modifying the query. You could prevent this by protecting that particular cell, if needed.

Lookup data based on drop down and secondary column google sheets

I'm trying to get data to appear based on a drop down and a secondary column. Here's the sample sheet: https://docs.google.com/spreadsheets/d/1LgHrze7bp0Epfw273Ylx3sVW98VDyd0sSQ1lYmZJUL4/edit?usp=sharing
I'm trying to get the Data sheet info to appear under the dropdown in the Worksheet sheet.
Any help would be great! Thanks!
Use hlookup like this. Put in B2 of Worksheet:
=transpose(arrayformula(hlookup(B1,Data!B1:G8,{2,3,4,5,6,7,8},false)))
If you put the cities in the rows, it would be much easier for you to add new cities. You can set the dropdown data validation to A2:A and the lookup to A2:H.
You can then add cities without adjusting anything. The formula the uses vlookup like:
=transpose(ARRAYFORMULA(vlookup(A1,RData!A2:H,{2,3,4,5,6,7,8},false)))
You can also do it with Google Apps Script that will expand to the number of cities and categories you have. I have added this to the shared sheet on the Script tab. It uses the RData tab as its source.
function onEdit(event) {
var sheet = event.source.getActiveSheet().getName()//get the sheet name
if(sheet=="Script" ){//if sheet name is Script
var ss= SpreadsheetApp.getActiveSpreadsheet()
var s=ss.getSheetByName("RData")//get the data sheet
var lr=s.getLastRow()//get the last row with data(city)
var lc=s.getLastColumn()//get the last column with data (category)
var rng=s.getRange(1, 1, lr, lc).getValues()//get the values
var dd= s.getRange("Script!A1").getValue()//get the selected city from the dropdown
var val=[]
var cat=[]
for(i=0;i<rng.length;i++){
if(dd==rng[i][0]){
for(j=1;j<rng.length+1;j++){
var t=rng[i][j]
val.push([rng[i][j]])
cat.push([rng[0][j]])
ss.getSheetByName("Script").getRange(1, 2, val.length,1).setValues(cat) //write the category values
ss.getSheetByName("Script").getRange(1, 3, val.length,1).setValues(val) //write the numbers
break //quit when the city is processed
}}}}}
Attached is a shared spreadsheet the shows all ways of solving your problem:
https://docs.google.com/spreadsheets/d/1h3kYpBTK8OpSVL5PGwQzYsFbIHmCaKx0G5Y8FXSdHH8/edit?usp=sharing

Force IMPORTRANGE to update at certain intervals

I saw several complains about the delay of updating data through IMPORTRANGE in Google Sheets but I need the opposite and don't want the second sheet to get updated automatically, just update at the end of the day for example.
The code is already like this:
=IMPORTRANGE("The Key","The_Page!B:D")
I hacked around this by, on both spreadsheets by creating a refresh loop using a NOW cell, with both spreadsheets crossreferencing each other's NOW cell.
When the original sheet gets appended with a form submission or something, it updates its own NOW cell, and reupdates own IMPORTRANGE cell. The second spreadsheet follows suit, updating its own NOW cell to provide the original sheet with the correct data. Because the second spreadsheet has updated itself, it also updates the main IMPORTRANGE which refreshes the data you want it to display in the first place, as well as the IMPORTRANGE cell which gets the NOW cell from the original spreadsheet
At least, I'm pretty sure that's how it works. All I know, and all I care about, frankly, is that it works
Maybe you need to use the script editor and write a simple function of the kind:
function importData()
{
var ss = SpreadsheetApp.getActiveSpreadsheet(); //source ss
var sheet = ss.getSheetByName("The_Page"); //opens the sheet with your source data
var values = sheet.getRange("B:D").getValues(); //gets needed values
var ts = SpreadsheetApp.openById("The Key"); //target ss - paste your key
ts.getSheetByName("Name of the target sheet").getRange("B:D").setValues(values);
}
And then add a time-driven trigger to this project (Resources > Current project's triggers > Add a new one).

Resources