Google Spreadsheets with Form Vinculated copy - google-drive-api

I'm trying to copy a spreadsheet with the responses of a form thought drive API.
https://developers.google.com/drive/v2/reference/files/copy
All files from my drive are working fine but only this spreadsheet that persist my form answers that create a duplicate form instead of only copying the spreadsheet itself.
You can try to reproduce the problem using the given Id. After copying you will notice that both spread sheet and form will be copied. This should not be a problem if I could erase the form but in the response of the copy procedure I don't get any advice about the form that is being copied together.
File id: 0Aqq-9JjR-lUydHRKVEJ2SThGMjJlVjVqczkyWlVCWUE
Please, help me. I'm desperate.

If you are only trying to copy the spreadsheet of the form, try this:
var fromSheet = ***whatever***;//this is the sheet attached to your form
var toSheet = ***whatever***;//this will be the sheet that you are copying to
var range = **whatever***;//this is the range you are copying over. If you are using dynamic ranges (ie varying number of rows), you may want to **getDataRange()** and **getLastRow** to build a more flexible range
function myCopyCat(){
myValues = fromSheet.getRange(range).getValues();//'copies' all of your data within range from fromSheet to an array
toSheet.getRange(range).setValues(myValues);//'pastes' all of your data into cells on toSheet
}

Related

Is there a way to auto save data in google sheet?

Is there a way to auto save data entered in a temp area (risk is a calculated value based on the values entered) on google sheet. I have a working space and all my logs is now needing to be saved for later review.
see sample sheet.
Created a sample data screenshot
Thanks
There's two ways to do it. You'll need to create a log of sorts and have the dashboard reference the bottom most entry. If you have App Script experience, that would be the better solution, however without it you could use the a Google Form for editing the dashboard. There wouldn't be any formulas alone that will work for this due to needing to hardcode the inputs, and formulas can only return values as arrays (mirror/change values in other cells).
You can use a Google Form that is linked to the spreadsheet so that someone has to submit the form with the inputs to change the dashboard. You would then use a =Max() function on the timestamp column, and then either Vlookup or Index(match()) to return the variables for the dashboard based off Max(timestamp).
The alternative method would be to create basically set of cells similar to the input table, and add a button that if clicked, takes, the values and updates them in the variables for the dashboard, but also logs them on another sheet. (It would be something like this)
Thank you all for the suggestions. I end up using the below script to accomplish the task.
function FormExec() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sinput = ss.getSheetByName("sheet1");
var soutput = ss.getSheetByName("sheet2");
var input = sinput.getRange(14, 3, 15).getValues();
var flatin = [].concat.apply([], input);
soutput.getRange(soutput.getLastRow()+1, 1,1, 15).setValues([flatin]);
soutput.insertRowAfter(soutput.getLastRow());
Logger.log(input);
}

Apps Script: how do you copy values from specific columns to a different spreadsheet via a time trigger, button press or sheet update?

I need to copy the values of multiple ranges of data from my main spreadsheet into another spreadsheet and then copy a different range of data back to the main spreadsheet.
Here are my example spreadsheets:
The main spreadsheet (two sheets: 1 - A sheet of pasted values alongside columns of user comments. 2 - A sheet with a button that runs the Apps Script)
The live data spreadsheet (pulls and formats the required data from other sheets).
Previously, I’ve used .copyTo() to copy the values of data and paste them elsewhere on the same spreadsheet. However, this method can’t be used to copy data to a different spreadsheet. Copying data from multiple ranges is also causing me issues. Here is my code:
function RefreshSheetData() {
// 1a. Run the script when a button is pressed (Main Spreadsheet - ‘Update report button!A3’)
// 1b. Run the script at a certain time (every Monday at 10 AM)
// 1c. Run the script when data in a sheet is replaced with new data via a formula – NOT POSSIBLE.
// 2. Copy email address and user comments (Main Spreadsheet - Editable report - J3:J & AM3:AR)
// 3. Clear the “Updated user comments” sheet below the header row (Live data Spreadsheet - Updated user comments - A2:G)
// 4. Paste the values (Live data Spreadsheet - Updated user comments - A2:G)
// 5. Clear the “Editable report” below the header rows (Main Spreadsheet - Editable report – B3:AR)
// 6. Copy the Live sheet (which should now include the most recent user comments via array vlookup) (Live data Spreadsheet - live data – A3:AQ)
// 7. Paste the values (Main Spreadsheet - Editable report - B3:AR)
// 8. Add the (United Kingdom) time and date (Main Spreadsheet - Update report button - A10)
// 9. Add the time and date (Live data Spreadsheet - Updated user comments - J1)
ScriptApp.newTrigger('RefreshSheetData')
.timeBased()
.onWeekDay(ScriptApp.WeekDay.MONDAY)
.atHour(10)
.create();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var startSheet = ss.getSheetByName('Editable report');
var sourceRange = startSheet.getRangeList(['J3:J', 'AM3:AR']);
var sourceValues = sourceRange.getValues();
var target = SpreadsheetApp.openById('1OHQHefYvE4vZZPr8jgziy_L3-UBf1WSoKzMWQ8LUz6w');
var targetSheet = target.getSheetByName('Updated user comments');
var clearTargetRange = targetSheet.getRange('A2:G').clearContent();
var targetRange = targetSheet.getRange('A2').setValues(sourceValues);
var liveTargetSheet = target.getSheetByName('Live data');
var liveSourceRange = liveTargetSheet.getRange('A3:AQ').getValues();
var clearMainRange = startSheet.getRange('B3:AR').clearContent();
var startRange = startSheet.getRange('B3').setValues(liveSourceRange);
SpreadsheetApp.getActive().getRange('A10').setValue(new Date());
targetSheet.getRange('J1').setValue(new Date())
}
Any tips would be appreciated. Thank you.
Part of my question was how to trigger a script when a formula output changes. That’s not possible. Neither .onEdit or .onChange triggers work as they only respond to user actions. They won't run when the value of an IMPORTRANGE or alternative formula changes.
Here some suggestions referring to each of the steps mentioned in your Apps Script file
1b. To run the script at a certain time: please use the installable trigger "Time driven"
1c. To run the script when data in the sheet is updated: please use the onEdit trigger
1d. You can combine all triggers by simply adding as many, as required.
To copy and paste data, you just need the methods getValues() and setValues(), which you were using already, the important thing is that you chose the "to copy" range correctly.
You can clear a range with clear()
See 3.
You can copy a sheet with copyTo(), however keep in mind that if using this method, your data in the copied sheet will automatically be updated if there is a change in the original sheet. If you want the values to remain static, you have to copy and paste them with copyValues() and setValues().
See 2. and 5.
See here how to get and format the date in Apps Script
Assign the date to a variable and use setValue()
I encourage you to try and build the script based on those steps
yourself, the Apps Script documentation provides you good reference
and guidance, how to do so. If you encounter specific problems during
one of the steps which you cannot solve with the documentation, feel
free to ask!

Automatically Copy Data Between Google Sheets

I am looking for some help automating copying data between google sheets.
I have a sheet called "Current Data". This sheet has data in columns A-F(with headers).
Column A contains usernames. Columns B-F will have formulas which pulls data from Instagram.com using
=VALUE(REGEXEXTRACT(IMPORTXML(
I got the formulas from this website:
https://www.benlcollins.com/spreadsheets/import-social-media-statistics/
There is another sheet called "Historical Data". This sheet contains the same columns as "Current Data" (A-F, with headers).
This sheet contains all data from the "Current Data" sheet, pasted daily.
My Current Process:
Open Sheet Navigate to "Current Data" sheet, copy values from A2-FXXX
Navigate to "Historical Data" sheet, scroll to next blank row, paste
data.
I am looking to automate this and have it occur daily.
I am using this script to automatically update my IMPORTXML function. This works great.
Periodically refresh IMPORTXML() spreadsheet function
I am copying the values from the Current Data sheet to the Historical
Data sheet, using this script.
copy and paste with google spreadsheet script.
This script also works, but it is only copying the first line of data to the destination. The script is also wiping the data from the Current Data sheet.
I removed the "source.clear ();" from the code, but the
data still gets wiped.
I also tried using this script, as some people mentioned users needed to use appendRow instead of copyTo. Still no luck with this code:
function moveValuesOnlyy() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getRange("CurrentData!A2:F100");
var destSheet = ss.getSheetByName("HistoricalData");
destSheet.appendRow(source.getValues());
}
In the end I am looking for the script to:
Find and copy the data range from the "Current Data" sheet, starting with A2.
Find next empty row on "Historical Data" sheet.
Paste as values.
Any help would be greatly appreciated, thank you!
Try this:
function copyPaste() {
var ss=SpreadsheetApp.getActive();
var srcsh=ss.getSheetByName('CurrentData');
var dessh=ss.getSheetByName('HistoricalData');
var srcrg=srcsh.getRange('A2:F100');
var data=srcrg.getValues();
var desrg=dessh.getRange(dessh.getLastRow() + 1,1,99,6);
desrg.setValues(data);
}

Changing cells of a google sheet file through another sheet

I have a big google sheet file with more thousand rows.
My goal is to create another smaller spreadsheet which imports a given line from the big database (just for show, I don't want to edit it oruse it) and after that I'd like to add some data to that given line.
I was able to do it with IMPORTRANGE function, but after I change to another row, the other rows I edited before don't keep the information I entered.
Also, I can't use my sheet to select a row in the big database and add a permanent data to it.
Do you have any idea how could I solve this problem? (Google script would be fine too, I really have no idea how to do this).
Thanks
Copy Selected Row
From one spreadsheet to another.
function copySelectedRow(){
var ss1=SpreadsheetApp.getActive();
var sh1=ss1.getSheetByName('Data');//Change data to the name of the sheet that you wish to copy from in ss1
var rg1=sh1.getRange(sh1.getActiveCell().getRow(),1,1,sh1.getLastColumn());//Select a cell in the row that you wish to copy and then run this function.
var vA1=rg1.getValues();
var ss2=SpreadsheetApp.openById('ID of Spreadsheet Copying To');
var sh2=ss2.getSheetByName('Data');//Change data to the name of the sheet that you wish to copy to in ss2
sh2.appendRow(vA1[0]);
}

Split data into different Google Sheets files

I have this one main file in Google Sheets. Now, I need to split this data such that its gets sent to " X " number of new files ( not a separate sheet). The data that is being sent is a subset of the data from the main file.
Is it possible to create these new workbooks and populate them with this subset data?
I tried creating new sheets, but the script is only working on creating it within the currently active sheet, and I cant find anything that is copying a range to a new sheet.
so i did figure out how to work around this thing.
now i didnt want to create a copy of the file. i wanted it to work somewhat like importrange.
var target2 = SpreadsheetApp.openById("Sheet url");
var target_sheet2 = target2.getSheetByName("Sheet1");
var source_rangefor2 = source_sheet.getRange("A7:C11");
var target_rangefor2 = target_sheet2.getRange("A1:C5");
var values = source_rangefor2.getValues();
target_rangefor2.setValues(values);
this copies the said range from the main sheet to the given range in the target sheet.
Hope this helps other people.