I'm wondering if when submitted, the data can be pasted to another separate file (and not tab). Do you think that there's a way?
You can access the file here : https://docs.google.com/spreadsheets/d/1FFlsfFyVyQobnbfAgQZA_r5IQeBhgOdK4cBpi5yo77U/edit?usp=sharing
Following code:
function myFunction(){
var ss = SpreadsheetApp.getActiveSpreadsheet();
var inputS = ss.getSheetByName("Input");
var outputS = ss.getSheetByName("Output");
var inputRng = inputS.getRange("A2:I2");
var values = inputRng.getValues();
inputRng.clearContent(); // clear input range
outputS.getRange(outputS.getLastRow()+1, 1, 1, values[0].length).setValues(values);
}
Explanation:
You can use openById(id) to access a different spreadsheet file by its id. As the documentation states:
For example, the spreadsheet ID in the URL
https://docs.google.com/spreadsheets/d/abc1234567/edit#gid=0 is
"abc1234567".
and assuming that the target spreadsheet has a sheet named "Output" then the following code will work. Otherwise, change it to the name of the target sheet.
Solution:
function ScriptBS() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var ts = SpreadsheetApp.openById(id); // put here the id of the target spreadsheet file
var inputS = ss.getSheetByName("Input");
var outputS = ts.getSheetByName("Output"); // get sheet Output of the target spreadsheet file
var inputRng = inputS.getRange("A2:I2");
var values = inputRng.getValues();
inputRng.clearContent(); // clear input range
outputS.getRange(outputS.getLastRow()+1, 1, 1, values[0].length).setValues(values);
}
Related
Right now I am using this formula to copy and paste to another Sheet. Though due to the file getting bigger and bigger, we want to place it in a seperate file which we dont really have to open.
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Kenmerken Script");
var pasteSheet = ss.getSheetByName("CopyPaste");
// get source range
var source = copySheet.getRange(3,3,300,6);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+2,1,500,4);
// copy values to destination range
source.copyTo(destination);
// clear source values
source.clearContent();
}
I tried to use getsheetbyURL instead. This did not work as it gave an error.
I also tried to find more information on https://developers.google.com/apps-script/reference/spreadsheet/sheet#copyTo(Spreadsheet). Though I cannot find an clear answer here.
I tried to add another "var" but "var sss = SpreadsheetApp.getActiveSpreadsheet. And then put pastesheet = sss.getsheetbyURL". This didnt work either.
I understand the things in the code which I have now. I only need to find the correct string.
I believe your goal is as follows.
You want to copy C3:H302 of a source sheet of Spreadsheet "A" to the last row of a destination sheet of Spreadsheet "B".
You want to use Spreadsheet URL for retrieving the Spreadsheet.
In this case, when your showing script is modified, how about the following modification?
Modified script:
function copyInfo() {
var destinationSpreadsheetUrl = "###"; // Please set your Spreadsheet URL.
var destinationSheetName = "###"; // Please set the destination sheet name.
// Source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Kenmerken Script");
// Destination sheet.
var dstSS = SpreadsheetApp.openByUrl(destinationSpreadsheetUrl);
var dstSheet = dstSS.getSheetByName(destinationSheetName);
var temp = copySheet.copyTo(dstSS);
temp.getRange(3, 3, 300, 6).copyTo(dstSheet.getRange(dstSheet.getLastRow() + 2, 1));
// clear source values
copySheet.getRange(3, 3, 300, 6).clearContent();
// Remove temp sheet.
dstSS.deleteSheet(temp);
}
When this script is run, the values of "C3:H302" are copied from the source sheet of the active Spreadsheet to the destination sheet of another Spreadsheet. And, "C3:H302" of the source sheet is cleared.
If you want to use Spreadsheet ID, please use SpreadsheetApp.openById() instead of SpreadsheetApp.openByUrl().
If you want to copy only the values, I thought that getValues and setValues can be also used.
References:
openByUrl(url)
copyTo(spreadsheet)
Added:
From your following reply,
The formula itself works which is great. But it seems to take the formulas and not only the values. See picture: i.imgur.com/VRYCuTP.png . This causes an error since this file is not aware of any sheet named X. Would there be a way to only copy the values?
In this case, how about the following sample script?
Sample script:
function copyInfo() {
var destinationSpreadsheetUrl = "###"; // Please set your Spreadsheet URL.
var destinationSheetName = "###"; // Please set the destination sheet name.
// Source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Kenmerken Script");
var temp1 = copySheet.copyTo(ss);
var r = temp1.getDataRange();
r.copyTo(r, { contentsOnly: true });
// Destination sheet.
var dstSS = SpreadsheetApp.openByUrl(destinationSpreadsheetUrl);
var dstSheet = dstSS.getSheetByName(destinationSheetName);
var temp2 = temp1.copyTo(dstSS);
temp2.getRange(3, 3, 300, 6).copyTo(dstSheet.getRange(dstSheet.getLastRow() + 2, 1));
// clear source values
copySheet.getRange(3, 3, 300, 6).clearContent();
// Remove temp sheets.
ss.deleteSheet(temp1);
dstSS.deleteSheet(temp2);
}
I was wondering if there was any way I could shorten the script below? As you can see, I copied the eight lines (B13-I13) to make a new one with a different row (B4-I14). However, I must continue it until row 23.
var values = [[formS.getRange("C7").getValue(),
formS.getRange("C8").getValue(),
formS.getRange("D9").getValue(),
formS.getRange("B13").getValue(), <--- shorten this
formS.getRange("C13").getValue(),
formS.getRange("D13").getValue(),
formS.getRange("E13").getValue(),
formS.getRange("F13").getValue(),
formS.getRange("G13").getValue(),
formS.getRange("H13").getValue(),
formS.getRange("I13").getValue(), ----> to this
formS.getRange("B14").getValue(), <--- shorten this
formS.getRange("C14").getValue(),
formS.getRange("D14").getValue(),
formS.getRange("E14").getValue(),
formS.getRange("F14").getValue(),
formS.getRange("G14").getValue(),
formS.getRange("H14").getValue(),
formS.getRange("I14").getValue(), ----> to this
]];
This is the script that I'm using..
function SaveData() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("Form");
var dataS = ss.getSheetByName("Data");
--> LOCATION OF THE SCRIPT ABOVE <---
dataS.getRange(dataS.getLastRow()+1,1,1,11).setValues(values);
ClearCell();
}
Please see the sample picture below where I encode the data.
This is the sample image of the sheet where the data were being saved.
SAMPLE SPREADSHEET
In your situation, how about the following modification?
Modified script:
function sample() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var formS = ss.getSheetByName("Form");
var dataS = ss.getSheetByName("Data");
var values = [
...formS.getRange("C7:C8").getValues().flat(),
formS.getRange("D9").getValue(),
...formS.getRange("B13:I23").getValues().flat()
];
dataS.appendRow(values);
}
From However, I must continue it until row 23., I used B13:I23.
In this modification, the values of "C7:C8" and "D9" and "B13:I23" of "Form" sheet are copied to the last row of "Data" sheet as one row.
Source:
Destination:
Hi everyone,
I have 2 google sheets, Source & Destination. I want to copy a range of data from Source sheet to Destination sheet by using google apps script. So the data should appear at the last row of the Destination sheet. This is my code:
function copyInfo() {
var ss = SpreadsheetApp.getActiveSpreadsheet();
var copySheet = ss.getSheetByName("Source");
var pasteSheet = ss.getSheetByName("Destination");
// get source range
var source = copySheet.getRange(3,1,1,10);
// get destination range
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
// copy values to destination range
source.copyTo(destination);
}
Seems like the script only allow me to do the copying within one google sheet (different tab) instead of 2 different google sheet. May I know how should I modified it so that the script able to find my destination google sheet and paste the data at the last row? Any help will be greatly appreciated!
Edit
I tried to use onEdit to triggered the script, however it seems like not working. Below is my script:
function onEdit(e) {
if (e.range.columnStart == 11 && e.range.rowStart == 3){
var ss = SpreadsheetApp.getActiveSpreadsheet();
const id ='1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw';
var copySheet = ss.getSheetByName("Sheet1");
var pasteSheet = SpreadsheetApp.openById(id).getSheetByName("Sheet1");
if (e.value == 'Submit'){
var source = copySheet.getRange(3,1,1,10);
const values = source.getValues();
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
destination.setValues(values);
}
}
}
Source:
https://docs.google.com/spreadsheets/d/1jnOvE-Dc7y9o7GsgN9fjOXZWlUCwiJayugX5q4Y3CA0/edit#gid=0
Destination:
https://docs.google.com/spreadsheets/d/1r5Hygl5ysahMi6DQ3duDR5L8c4aGX_0CJba7lXxnejw/edit#gid=0
Use Range.getValues() and Range.setValues()
const id ='ID of paste Spreadsheet';
var pasteSheet = SpreadsheetApp.openById(id).getSheetByName("Destination");
var destination = pasteSheet.getRange(pasteSheet.getLastRow()+1,1,1,10);
const values = source.getValues();
destination.setValues(values);
The method getSheetByName is used to define a sheet within a spreadsheet. To define a specific spreadsheet, you should use openById and associate the spreadhseet ID (https://docs.google.com/spreadsheets/d/{spreadsheet ID}/edit).
Define your source sheet as:
var copysheet = SpreadsheetApp.openById('enter id for Source').getSheetByName('Sheet 1')
Define your destination sheet as:
var pastesheet = SpreadsheetApp.openById('enter id for Destination').getSheetByName('Sheet 1')
If I understand correctly, your goal is the following:
/* Every time a user sets the value of cell K3 in source spreadsheet
to "submit". Copy all the values of row 3 and paste them into the
destination sheet under the last row. Then delete the source values
and set the value of K3 to "Clear" again. */
You need a installed trigger and a function to copy the values when triggered. So lets do that step by step.
Example:
First lets create a script that is bound to your source SS. Let's edit your copyInfo() function so that it takes the destination spreadsheet Id as parameter.
function copyTo(destinationSSId) {
// Open source SS at tab named "Source"
let copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source");
// Open destination SS at tab named "Destination"
let pasteSheet = SpreadsheetApp.openById(destinationSSId).getSheetByName("Destination");
// Get source values
let sourceValues = copySheet.getRange(3, 1, 1, 10).getValues();
// Set values to destination range
pasteSheet.getRange(pasteSheet.getLastRow() + 1, 1, 1, 10).setValues(sourceValues);
// Log the pasted values from the last row
console.log("Last row pasted: " + pasteSheet.getRange(pasteSheet.getLastRow(), 1, 1, 10).getValues()[0]);
}
Make a function to clear the source values and reset the "form".
function clearSubmission() {
let copySheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Source");
copySheet.getRange(3, 1, 1, 10).clearContent()
copySheet.getRange(3, 11, 1, 1).setValue("Clear");
console.log("Source submitted and cleared.")
}
Set up the function to trigger.
function myTriggeredFunction(e) {
console.log(JSON.stringify(e.range), e.value)
if (e.range.columnStart == 11 && e.range.rowStart == 3) {
if (e.value == 'Submit') {
let destinationSSId = '{YOUR DESTINATION SS ID}';
copyTo(destinationSSId);
clearSubmission();
}
}
}
Install the trigger as "From spreadsheet - On edit" to run the function myTriggeredFunction by going to the "Triggers" tab in your project.
I want to have my app script generate output to a SPECIFIC sheet, not the first sheet in the document.
i am looking to do the following:
Have 2 app scripts run on timers for a single google sheet document
App Script 1 will put data into sheet1, named "order_1"
App script 2 will put data into sheet2, named "orders_2"
my code currently puts the output into the active sheet, which is always the first sheet in the document
function import_Completed_orders_FromGmail() {
var threads = GmailApp.search('subject: "orders"');
var message = threads[0].getMessages()[0];
var attachment = message.getAttachments()[0];
// Is the attachment a CSV file
attachment.setContentTypeFromExtension();
if (attachment.getContentType() === "text/csv") {
var sheet = SpreadsheetApp.getActiveSheet();
var csvData = Utilities.parseCsv(attachment.getDataAsString(), ",");
// Remember to clear the content of the sheet before importing new data
sheet.clearContents().clearFormats();
sheet.getRange(1, 1, csvData.length,
csvData[0].length).setValues(csvData);
}
}
is there a way to define "ActiveSheet" by name? or to specify a specific sheet to put this output?
Use getSheetByName(name) method from Class Spreadsheet.
In order to get a Spreadsheet object you will have to use one of the SpreadsheetApp methods to open a spreadsheet like openById(id), open(file), openByUrl(url), getActiveSpreadheet().
You can get both sheet separately and use them in the code.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet1 = ss.getSheetByName("order_1");
var sheet2 = ss.getSheetByName("order_2");
I am trying to create a Google Sheet template where a user can enter a value (Project URN) in a cell and run a script to save the template as a sheet, renamed with the Project URN.
I have created a test Google Sheet here https://docs.google.com/spreadsheets/d/1IPpQyYHl_TtNa1lGpmu4dlzwTFjOotV2OcnxFT84iUk/edit?usp=sharing
This sheet has the following script, but I cant get the renaming function to correctly work.
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("0B7RrbZuJGLlha3NvM3ZqcTY3MDA");
DriveApp.getFileById(sheet.getId())
.(makeCopy(values = SpreadsheetApp.getActiveSheet()
.getRange(2, 2).getValues()), destFolder);
} //END function saveAsSpreadsheet
I did also find the following article but wasn't able to turn it to my use. However, the secret must be here somewhere! Rename worksheet to cell value keeping format
Kitten
You almost have it. Please try this:
function myFunction() {
var ss = SpreadsheetApp.getActiveSpreadsheet(); // Get spreadsheet
var id = ss.getId(); // Get spreadsheet ID
var sstocopy = DriveApp.getFileById(id); //Get spreadsheet with DriveApp
var sheet = ss.getActiveSheet(); // Grab the sheet in order to find the name of the new spreadsheet to be created
var sheet_name = sheet.getRange("A1").getValue(); // Get the value, in this case: Project Urn
var folder = DriveApp.getFolderById("XXXX"); // Get the folder where the sheet needs to be placed.
sstocopy.makeCopy(sheet_name,folder); // Make the copy of the sheet in that folder.
}
Let me know if you have any questions.