How to copy one google sheet to different another google sheet - google-apps-script

I do have two different google sheet, I need to copy say sheet Test from one to another using script, but getting error, any thought on this?
var source_sheet= "https://address of the otgoogle sheet tho be copied over")"
//here I'm on another google sheet, add a menu to copy the source from above
var ui = SpreadsheetApp.getUi();
//var ss = SpreadsheetApp.getActiveSpreadsheet();
var result = ui.prompt(
'Add New Sheet',
'Please enter Name',
ui.ButtonSet.OK);
var button = result.getSelectedButton();
var text = result.getResponseText();
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getActiveSheet();
sheet.copyTo(link_to_src).setName(text);
Getting below error:
Exception: The parameters (String) don't match the method signature for SpreadsheetApp.Sheet.copyTo. (line xx, file "copy-rename")DetailsDismiss
Any help will be appreciated in advance..

You want to copy a sheet of the source Spreadsheet, which is not the active Spreadsheet to the active Spreadsheet, which run the script.
source_sheet is the URL of Spreadsheet like https://docs.google.com/spreadsheets/d/{spreadsheetId}/edit.
When you run the script, you want to input the sheet name in source Spreadsheet using a dialog and copy the sheet to the active Spreadsheet.
I could understand like above. If my understanding is correct, how about this modification?
Modified script
Please modify your script as follows. Before you run the script, please set the destination Spreadsheet URL to source_sheet.
From:
var sheet = ss.getActiveSheet();
sheet.copyTo(link_to_src).setName(text);
To:
var sourceSheet = SpreadsheetApp.openByUrl(source_sheet).getSheetByName(text);
if (sourceSheet) {
sourceSheet.copyTo(ss).setName(text).activate();
} else {
ui.alert(text + " sheet was not found.");
}
When you run the script, a dialog is opened. When a sheet name of the source Spreadsheet is inputted, the sheet is copied to the current active Spreadsheet, then, the copied sheet is activated.
If the sheet name which is not included in the source Spreadsheet is inputted, a dialog is opened.
References:
openByUrl(url)
copyTo(spreadsheet)

Related

Appscript that creates a new worksheet when new data is entered in Google sheet

I have an app script code that Creates a new workbook when new data is entered in Google Sheet. The code is below.
function myFunction() {
// The code below creates a new spreadsheet "New Sheet Name" and logs the URL for it
var ssNew = SpreadsheetApp.create();
Logger.log(ssNew.getUrl());
}
I want the new spreadsheet name to be the last data entered in Column A and also the URL for the new workbook to be pasted in Column J of the same row as the last data in Column A
From I have an app script code that Creates a new workbook when new data is entered in Google Sheet., I thought that you might be running the script with the installable OnEdit trigger. But from your showing script, I thought that you might have wanted to directly run with the script editor. If my understanding is correct, how about the following modification?
Modified script:
function myFunction() {
var sheet = SpreadsheetApp.getActiveSheet();
var lastRow = sheet.getLastRow();
var title = sheet.getRange("A" + lastRow).getValue();
var ssNew = SpreadsheetApp.create(title);
sheet.getRange("J" + lastRow).setValue(ssNew.getUrl());
}
When this script is run, the title is retrieved from the column "A" of the last row. And, the URL of created Spreadsheet is put in the column "J" of the same row.
Reference:
getLastRow()
Creating a new Spreadsheet
function myFunction() {
const ss = SpreadsheetApp.getActive();
const sh = ss.getActiveSheet();
const nss = SpreadsheetApp.create(sh.getRange(sh.getLastRow(), 1).getDisplayValue())
sh.getRange(sh.getLastRow(), 10).setValue(nss.getUrl());
}

Open Google Spreadsheet via Google Apps Script after creating a copy

I am creating a copy of my google spreadsheet, and now want to close the current spreadsheet and open the one i created a copy. I know i can open it using the sheet id, but i dont know the sheet id yet because I want to achieve this using one macro!
current code -
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxxxxxxxxxxxxxxxxx");
DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
//want to open this copied file
}
By modified your code a little bit, then you can obtain the copy sheet Id with newsheet.getId() function:
function saveAsSpreadsheet(){
var sheet = SpreadsheetApp.getActiveSpreadsheet();
var destFolder = DriveApp.getFolderById("xxxxxV6N-A4Z77f9OijHtAe2VhAU");
var newsheet = DriveApp.getFileById(sheet.getId()).makeCopy("Test File", destFolder);
Logger.log(newsheet.getId())
}

How to get the specific tab to be accessed

in my script , I am attempting the execute my code on a specific tab.
Here is my code snippet
function sendalert() {
var sheet = SpreadsheetApp.getActiveSheet();
My google sheet has 6 tabs, but I would like my script to run always on the tab named "Operator". How do I enforce this?
I believe your goal as follows.
You want to run the script for the sheet with the sheet name of Operator in the active Spreadsheet.
For this, how about this answer?
Modification points:
In order to access to the specific sheet using the sheet name, you can use getSheetByName(name).
When getActiveSheet() is used, the opened sheet in the browser is used. If the browser is not opened, the sheet of 1st tab is used.
Modified script:
When your script is modified, please modify as follows.
function sendalert() {
var sheetName = "Operator";
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
// do something.
}
References:
getActiveSpreadsheet()
getSheetByName(name)

Copying / transferring from one spreadsheet to another code based

I'm trying to copy / transfer a sheet from one spreadsheet to another spreadsheet. I have tried various methods and .copyTo seems to be the best and most efficient way.
.copyTo works well but I'm struggling to send to a specific sheet...
Here is my code:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var ss = SpreadsheetApp.getActiveSpreadsheet();
sheetA.copyTo(destination); // I tried renaming .setName(each);
}
So if I only use sheetA.copyTo(destination); it simply creates a copy sheet, like Copy of the_souce_sheet_name.
If I try renaming to make it a specific name I will get error after running for the second time that the sheet already exists in destination spreadsheet.
What I really need to achieve is that the function from source spreadsheet copies data from the source sheet to always the same sheet in destination spreadsheet.
Perhaps .copyTo is not the right way to do it?
Any suggestions and code will help please!
The reason for receiving data on a exact sheet in the destination spreadsheet is because I have a trigger On change that executes another script to work with the new incoming data.
You want to overwrite the source sheet of Spreadsheet A to the destination sheet of Spreadsheet B.
You want to keep the sheet name of var each = "Data_Incoming".
If my understanding is correct, how about this answer? I would like to propose 2 samples. So please select one of them for your situation.
Sample script 1:
The flow of this sample script is as follows.
each sheet of Spreadsheet B is deleted.
Source sheet of Spreadsheet A is copied to the destination sheet of Spreadsheet B.
Sheet name of the copied sheet of Spreadsheet B is modified to each.
Modified script:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destination.deleteSheet(destSheet);
}
sheetA.copyTo(destination).setName(each);
}
Sample script 2:
The flow of this sample script is as follows.
Copy the source sheet of Spreadsheet A to Spreadsheet B.
If the sheet with the sheet name of each is not existing in Spreadsheet B, the copied sheet is renamed to each.
If the sheet with the sheet name of each is existing in Spreadsheet B, the source sheet is copied to the Spreadsheet B as Copy of ###.
each sheet is cleared.
All values, formulas and formats of the copied sheet are copied to each sheet.
Delete the copied sheet.
Modified script:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
}
Note:
In this sample script, each sheet of the Spreadsheet B (destination Spreadsheet) is deleted. So please be careful this.
So at first, as a test, I recommend to use a sample Spreadsheet.
References:
copyTo(spreadsheet) of Class Sheet
copyTo(destination) of Class Range
deleteSheet(sheet)
If I misunderstood your question and this was not the result you want, I apologize.
Added:
You want to run the script of destination Spreadsheet when the source values are copied to the destination Spreadsheet using the script in the source Spreadsheet.
The following sample script is for achieving above situation.
Script of destination Spreadsheet:
At first, the script of destination Spreadsheet is prepared.
Script:
function doGet() {
sample(); // This is the function that you want to run when the source values are copied.
return ContentService.createTextOutput();
}
After copy and paste above script to the script editor of destination Spreadsheet, please do the following flow.
Deploy Web Apps.
On the script editor, Open a dialog box by "Publish" -> "Deploy as web app".
Select "Me" for "Execute the app as:".
Select "Anyone, even anonymous" for "Who has access to the app:".
Click "Deploy" button as new "Project version".
Automatically open a dialog box of "Authorization required".
Click "Review Permissions".
Select own account.
Click "Advanced" at "This app isn't verified".
Click "Go to ### project name ###(unsafe)"
Click "Allow" button.
Copy "Current web app URL:".
It's like https://script.google.com/macros/s/#####/exec.
Click "OK".
Script of source Spreadsheet:
As the next step, the script of source Spreadsheet is prepared as follows. In this sample, the sample 2 was used. I think that you can also use the sample 1.
Script:
function TransferDataOut() {
var source = SpreadsheetApp.getActiveSpreadsheet();
var sheetA = source.getSheets()[0]; //sheet source number
var destination = SpreadsheetApp.openById('the destination sheet');
var each = "Data_Incoming";
var copiedSheet = sheetA.copyTo(destination);
var destSheet = destination.getSheetByName(each);
if (destSheet) {
destSheet.clear();
var srcRange = copiedSheet.getDataRange();
srcRange.copyTo(destSheet.getRange(srcRange.getA1Notation()));
destination.deleteSheet(copiedSheet);
} else {
copiedSheet.setName(each);
}
// Added
var url = "https://script.google.com/macros/s/###/exec"; // Please set the retrieved URL of Web Apps.
UrlFetchApp.fetch(url);
}
Note:
By above settings, when TransferDataOut() of the source Spreadsheet was run, doGet() of the destination Spreadsheet is run by UrlFetchApp.fetch(url).
References:
Web Apps
Taking advantage of Web Apps with Google Apps Script

Create template Google Sheet with script to rename from cell value

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.