Tool to Select Multiple Layers, Export to Single Excel with multiple worksheets

818
3
07-20-2022 10:49 AM
smevans88
New Contributor II

Hello All,

We have created a gp tool (using Python) to select features from multiple layers, export them to individual CSV files, then combine them as separate worksheets in an Excel workbook. Works great locally in ArcMap, then after publishing, and bringing into WAB for Poral, it errors out due to limitations of the WAB Portal environment.

Is this a functionality that can be used in the Enhanced Search widget?

If not, is this easy enough to do in JavaScript? Below is the code for a Python solution, which is very simple:

##Import Modules
import arcpy
import glob, os
import pandas as pd
import xlsxwriter
from pandas import DataFrame, ExcelWriter
import csv

##Allow outputs to be overwritten
arcpy.env.overwriteOutput = True

# CSV Locations folder path
csv_Output_dir = arcpy.env.scratchFolder

## Get names of layers that have been selected
## If features from multiple layers are selected, a List is returned, separated by semicolon
in_featureclassInputs = arcpy.GetParameterAsText(0)
arcpy.AddMessage("Input Layer Names: " + str(in_featureclassInputs+"\n"))

## Loop through each layer in List to create individual CSV files
for FeatClass in in_featureclassInputs.split(';'):

     ## Strip out unnecessary quotes and slashes
     FeatClassStripped = FeatClass.replace("'","")
     CSV_Name = FeatClassStripped.split("\\")[-1]+".csv"

     ##Arcpy table conversion tool being used here
     arcpy.conversion.TableToTable(in_rows=FeatClassStripped, out_path=csv_Output_dir,     out_name=CSV_Name)

### Create Excel File and Combine CSVs into it
Excel_workbook = pd.ExcelWriter(arcpy.env.scratchFolder+"\GIS_Asset_List.xlsx", engine='xlsxwriter')
     for csv_file in glob.glob(os.path.join(arcpy.env.scratchFolder, '*.csv')):
     read_file = pd.read_csv(csv_file)
     sheetTitle = csv_file.split("\\")[-1].replace(".csv","")
     read_file.to_excel (Excel_workbook, sheet_name=sheetTitle)

## Save Excel Workbook and create link for Excel download
Excel_workbook.save()
arcpy.SetParameterAsText(1, arcpy.env.scratchFolder+"\GIS_Asset_List.xlsx"

0 Kudos
3 Replies
JamesCrandall
MVP Frequent Contributor

One idea/option is to publish the Tool you've created as a new Geoprocessing Service onto your ArcGIS Server site, at that point it's just a REST endpoint that you can execute from just about anything that has web access.  We do this in some of our WAB widgets with the javascript api's Geoprocessor task 

0 Kudos
smevans88
New Contributor II

Thanks James. This actually seems doable. I will start looking at this solution and provide updates. Thanks!

0 Kudos
ahargreaves_FW
Occasional Contributor III

@JamesCrandall I am attempting to do what you say with a simple GP model that exports features to shapefiles. It works fine in Pro, and allows me to publish it as a service. However, when I use that service in the WAB GP tool my input and output parameters fail to show. How do I expose these?

ahargreaves_FW_0-1672755681561.png

 

ahargreaves_FW_1-1672755706095.png

 

0 Kudos