Specify a worksheet to combine - Office Scripts

Hervé 5 Reputation points
2023-04-20T07:33:28.4766667+00:00

The two sample codes below are retrived from Combine worksheets into a single workbook - Office Scripts documentation https://learn.microsoft.com/en-us/office/dev/scripts/resources/samples/combine-worksheets-into-single-workbook For a workbook that contains multiple worksheets, is it possible to combine a specific worksheet that has the same name or the first/last sheet in the workbook? For example, I have two Excel files called A and B. A has two sheets, called sheet 1 and sheet 2, while B has one sheet called sheet 1. After running the example flow, A.sheet1, A.sheet2 and B.sheet1 are all added to the combined Excel file. My motivation is to get rid of A.sheet2.

Office Development
Office Development
Office: A suite of Microsoft productivity software that supports common business tasks, including word processing, email, presentations, and data management and analysis.Development: The process of researching, productizing, and refining new or existing technologies.
4,367 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. peiye zhu 165 Reputation points
    2023-04-21T10:23:48.1366667+00:00

    This is a sample of combine specific worksheets in multiple workbooks. https://club.excelhome.net/forum.php?mod=viewthread&tid=1659202&page=1&mobile=

    0 comments No comments

  2. peiye zhu 165 Reputation points
    2023-04-21T10:27:31.6+00:00

    network block repeat post

    0 comments No comments

  3. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

  4. Yutao Huang - MSFT 701 Reputation points Microsoft Employee
    2023-04-21T16:56:57.6933333+00:00

    You can probably tweak the first script in the sample code to only export the worksheets you are interested in.

    For example:

    /**
     * This script returns the values from the used ranges on each worksheet.
     */
    function main(workbook: ExcelScript.Workbook): WorksheetData[] {
    	// Create an object to return the data from each worksheet.
    	let worksheetInformation: WorksheetData[] = [];
    
    	// Add the names of the worksheets to be exported. Here only "Sheet1" is included. Add more names to the list if needed.
    	const sheetNamesToBeIncluded = ["Sheet1"];
    
    	// Get the data from every worksheet, one at a time.
    	workbook.getWorksheets().forEach((sheet) => {
    		const sheetName = sheet.getName();
    		// Only export worksheets we want
    		if (sheetNamesToBeIncluded.includes(sheetName)) {
    			let values = sheet.getUsedRange()?.getValues();
    			worksheetInformation.push({
    				name: sheetName,
    				data: values as string[][]
    			});
    		}
    	});
    
    	return worksheetInformation;
    }
    
    // An interface to pass the worksheet name and cell values through a flow.
    interface WorksheetData {
    	name: string;
    	data: string[][];
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.