-
-
Notifications
You must be signed in to change notification settings - Fork 315
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3059 from IamEzio/perfect-excel
Added functionality to import perfect Excel
- Loading branch information
Showing
11 changed files
with
162 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
import pandas | ||
|
||
from db.tables.operations.alter import update_pk_sequence_to_latest | ||
from mathesar.database.base import create_mathesar_engine | ||
from db.records.operations.insert import insert_records_from_excel | ||
from db.tables.operations.create import create_string_column_table | ||
from db.tables.operations.drop import drop_table | ||
from mathesar.imports.utils import get_alternate_column_names, process_column_names | ||
from psycopg2.errors import IntegrityError, DataError | ||
|
||
from mathesar.state import reset_reflection | ||
|
||
|
||
def insert_records_from_dataframe(name, schema, column_names, engine, comment, dataframe): | ||
table = create_string_column_table( | ||
name=name, | ||
schema=schema.name, | ||
column_names=column_names, | ||
engine=engine, | ||
comment=comment, | ||
) | ||
|
||
insert_records_from_excel( | ||
table, | ||
engine, | ||
dataframe, | ||
) | ||
return table | ||
|
||
|
||
def create_db_table_from_excel_data_file(data_file, name, schema, comment=None): | ||
db_name = schema.database.name | ||
engine = create_mathesar_engine(db_name) | ||
dataframe = pandas.read_excel(data_file.file.path) | ||
column_names = process_column_names(dataframe.columns) | ||
try: | ||
table = insert_records_from_dataframe(name, schema, column_names, engine, comment, dataframe) | ||
update_pk_sequence_to_latest(engine, table) | ||
except (IntegrityError, DataError): | ||
drop_table(name=name, schema=schema.name, engine=engine) | ||
column_names_alt = get_alternate_column_names(column_names) | ||
table = insert_records_from_dataframe(name, schema, column_names_alt, engine, comment, dataframe) | ||
|
||
reset_reflection(db_name=db_name) | ||
return table |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters