I am trying to write a function where a specified workbook is opened and then we need to copy existing sheet and make a copy of it in the same workbook and rename that worksheet.
I saw this feature is implemented In VBA using the code below.
Worksheets("Sheet1").Copy After:=Worksheets("Sheet3")
In Javascript I am trying to do
var xlApp = new ActiveXObject("Excel.Application");
var xlWorkbooks = xlApp.Workbooks;
var xlWorkbook = xlWorkbooks.Open(xlFileName);
var xlWorksheets = xlWorkbook.Worksheets;
var xlWorkSheet = xlObj.getWorksheet("PRC1.1");
xlWorkSheet.copy();
This code is creating a copy in new workbook but I want in the same workbook.
Any Answers would be highly appreciated.
Thanks in Advance.
Related
Im trying to .makecopy of a single tab or sheet from a spreadsheet with multiple tabs, Im able to make a copy of the entire sheet however this includes information the end user does not need to see.
I've seen some threads about creating a new spreadsheet within apps script then coping over the detail although formatting gets messy, I know there is a way to fix this but i'd like to know if there is a method as simple as the one i use currently to copy an entire sheet.
//Written like a true noob.
function Createcopy() {
//Base
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName('Review')
var date = sheet.getRange(1,3).getValue(); // Probably wont need.
//Folder detail
var folderid = '1iEILmAp3JiOTiPjFbDhv0bAfgu6AWhtd'
var Folder = "https://drive.google.com/drive/folders/"+folderid
Logger.log(folderid)
//Create copy
var title = sheet.getRange(1,4).getValue();
Logger.log(title)
var filename = title
var destFolder = DriveApp.getFolderById(folderid);
var createdcopy = DriveApp.getFileById(ss.getId()).makeCopy(filename, destFolder);
var url = createdcopy.getUrl()
Logger.log(url)
}
I believe your goal is as follows.
You want to copy the specific sheet in a Google Spreadsheet to a new Google Spreadsheet.
You want to put the new Spreadsheet in the specific folder.
You want to retrieve the URL of the new Spreadsheet.
Modification points:
As you say, your script copies the whole Spreadsheet.
In this case, I would like to propose the following flow.
Retrieve source sheet.
Copy the source sheet to a new Spreadsheet and remove the default empty sheet.
Move a new Spreadsheet to the destination folder.
Retrieve the URL of a new Spreadsheet.
When this flow is reflected in your script, it becomes as follows.
Modified script:
function Createcopy() {
var sheetName = 'Review'; // Please set the source sheet name.
var folderId = '###'; // Please set the destination folder ID.
// 1. Retrieve source sheet.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var title = sheet.getRange(1, 4).getValue(); // New Spreadsheet title. This is from your script.
// 2. Copy source sheet to a new Spreadsheet and remove the default empty sheet.
var newSS = SpreadsheetApp.create(title);
sheet.copyTo(newSS).setName(sheetName);
newSS.deleteSheet(newSS.getSheets()[0]);
// 3. Move a new Spreadsheet to the destination folder.
var dstFolder = DriveApp.getFolderById(folderId);
DriveApp.getFileById(newSS.getId()).moveTo(dstFolder);
// 4. Retrieve URL of a new Spreadsheet.
var url = newSS.getUrl();
Logger.log(url)
}
When this script is run, the source sheet is copied to a new Spreadsheet. And, at a new Spreadsheet, the default empty sheet is removed. By this, a Spreadsheet including the specific sheet is obtained.
Note:
In this case, if the source sheet includes the formulas for loading the values from another sheet, an error occurs. Please be careful about this. And also, for example, when the source sheet includes the custom function, I think that the following script might be able to be used.
function Createcopy() {
var sheetName = 'Review'; // Please set the source sheet name.
var folderId = '###'; // Please set the destination folder ID.
var ss = SpreadsheetApp.getActiveSpreadsheet();
var sheet = ss.getSheetByName(sheetName);
var title = sheet.getRange(1, 4).getValue(); // New Spreadsheet title. This is from your script.
var dstFolder = DriveApp.getFolderById(folderId);
var newSSFile = DriveApp.getFileById(ss.getId()).makeCopy(title, dstFolder);
var newSS = SpreadsheetApp.open(newSSFile);
newSS.getSheets().forEach(s => {
if (s.getSheetName() != sheetName) newSS.deleteSheet(s);
});
var url = newSS.getUrl();
Logger.log(url)
}
References:
create(name)
copyTo(spreadsheet) of Class Sheet of Class SpreadsheetApp
Using office.js, is there a way to create a new workbook and then edit the new workbook?
I thought about these two options but couldn't find a way to implement any of them:
(preferred): Get an object reference when creating the new workbook via Excel.createWorkbook(), similar to VBA, like
var newWorkbook = Excel.createWorkbook();
newWorkbook.doStuff();
Create the data in a new Worksheet within the source Workbook and then save this Worksheet as a new Workbook
I would like to move a sheet ("HeaderSheet") from one workbook ("HeaderSpreadsheet") to another workbook ("ReportSpreadsheet").
Is it possible??
I tried the following without success:
var HeaderSpreadsheet = SpreadsheetApp.openByUrl('Url_1')
var ReportSpreadsheet = SpreadsheetApp.openByUrl('Url_2')
HeaderSheet = HeaderSpreadsheet.getSheetByName("Header")
ReportSpreadsheet.insertSheet('Data', 1, {template: HeaderSheet})
Alternatively, could I copy the content from the Header workbook to the Report one?
Thank you very much for your help
I found my question was already solved.
This may be useful:
Google Apps Script, copy one spreadsheet to another spreadsheet with formatting
I am looking to retrieve cells from an excel spreadsheet using JavaScript, to use the values in an HTML page. Currently, I do not know how to use SheetJS, and I am not able to use it to parse the worksheets.
What I am looking to do is: Retrieve Cell A1 -> Create a variable with the value of cell A1
Could anyone help me access the worksheet from JS and then get the value from an XLSX sheet? I have to use Excel for the purpose of this project.
Thanks!
function ReadData(cell, row) {
var excel = new ActiveXObject("Excel.Application");
var excel_file = excel.Workbooks.Open("C:\\RS_Data\\MyFile.xls");
var excel_sheet = excel.Worksheets("Sheet1");
var data = excel_sheet.Cells(cell, row).Value;
document.getElementById('div1').innerText = data;
}
I had the same error.
You should try your path:
excel.Workbooks.Open("C://RS_Data//MyFile.xls");
// --> instead of --> \
So I am using a SharePoint web part (HTML Form Web Part) that I've built to query an excel file that is hosted on our Sharepoint site. I wrote javascript to create an Excel ActiveX object, and I can search the worksheet just fine.
However, I need to have it search for an exact match (the whole cell) not just part. I know the code that I need, I just can't make it work. I need to find out how to create the correct object type for the "xlWhole" argument of the "Find()" function.
The line of code I am having trouble with is commented out, because that didn't work. Any insight? I've seen the Range.Find method, but I just can't make it work.
var excel = new ActiveXObject("Excel.Application");
var wb = excel.Workbooks.Open("workbook path");
var ws = wb.Worksheets("worksheet name");
var ws = wb.ActiveSheet;
//var cell = ws.Cells.Find(str,excel.XlLookAt.xlWhole);
var cell = ws.Cells.Find(str);
foundRow = cell.Row;
This worked for me:
function searchExcel()
{
var excel =new ActiveXObject("Excel.Application");
excel.visible=true;
var wb = excel.workbooks.open("D:\\Analysis\\tmp\\Book1.xlsx");
var ws = wb.sheets(1);
var str="Value1";
// .Find(What, After, LookIn, LookAt, SearchOrder, SearchDirection,
// MatchCase, MatchByte, SearchFormat)
var cell = ws.Cells.Find(str,ws.Cells(1),-4163,1)
alert(cell?cell.Row:(str+" not found"));
excel.quit();
}
I guess you cannot skip over parameters when using COM from js (but you can leave them off the end).