Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add plugin-copy-column #611

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open

add plugin-copy-column #611

wants to merge 2 commits into from

Conversation

syshinnn
Copy link

Please explain how to summarize this PR for the Changelog:

This plugin allows users to copy all the values from one column to another within a sheet.

You'll need to add the action to your sheet like below:

      {
        operation: "copy-column",
        mode: "foreground",
        label: "Copy Column",
        description: "Copy column values from one column to another.",
        primary: true,
      },

And add a listener like below:
listener.use(copyColumn());

Copy link
Contributor

coderabbitai bot commented Sep 12, 2024

Walkthrough

The changes introduce a new functionality for copying values between columns within a workbook in the Flatfile API. A new file, copy-column.ts, implements the copyColumn function, which sets up event listeners for workbook creation and job readiness, allowing users to specify source and target columns for data copying. An additional file, index.ts, re-exports the functionalities from copy-column.ts, streamlining access for users.

Changes

Files Change Summary
plugins/copy-column/src/copy-column.ts Implements the copyColumn function and event listeners for copying column data. Adds utility functions for record transformation and sheet actions. Exports types and functions for external use.
plugins/copy-column/src/index.ts Re-exports all entities from copy-column.ts to provide a single access point for the new functionality.

Possibly related PRs

  • exporter: pivot table report generator #642: The job:ready event is utilized in both the main PR and this retrieved PR, indicating a shared mechanism for handling job events in the Flatfile ecosystem, specifically for processing data and generating outputs.

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

plugins/copy-column/src/copy-column.ts Outdated Show resolved Hide resolved
sheets.forEach(async (sheet) => {
const loadAllFields = await getAllFields(sheets, sheet.slug);
setSheetActions(sheet, [
copyColumnValuesBlueprint(loadAllFields),
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Neat to populate all the field enums for each sheet!
Does this workbook:created event get triggered for File based Workbooks? Would we want to add the Action on every workbook:created or sheet:created?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not seeing sheet:created when creating a space. What do you mean by File based Workbooks?

plugins/copy-column/src/copy-column.ts Outdated Show resolved Hide resolved
plugins/copy-column/src/copy-column.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 3

🧹 Outside diff range and nitpick comments (1)
plugins/copy-column/src/copy-column.ts (1)

94-95: Remove debug logging statements from production code

The console.log statements in getAllFields appear to be for debugging purposes. It's best practice to remove or disable such statements in production to prevent unnecessary console output.

Apply this diff to remove the debug statements:

  const _fields = sheets.reduce((acc, sheet) => {
-   console.log("in getallfields, sheet.config.fields here: ")
-   console.log(sheet.config)
    if (sheet.config.slug == sheetSlug) {
      acc.push(
        ...sheet.config.fields
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

📥 Commits

Files that changed from the base of the PR and between 7cf84c1 and e36a41b.

📒 Files selected for processing (1)
  • plugins/copy-column/src/copy-column.ts (1 hunks)
🧰 Additional context used

Comment on lines +88 to +89
console.log({ e });
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🛠️ Refactor suggestion

Enhance error handling in the 'setSheetActions' function

Currently, the error is logged using console.log({ e });, which might not be sufficient for debugging and monitoring. Consider implementing a more robust error handling mechanism, such as rethrowing the error or logging it using a dedicated logging service.

Apply this diff to improve error handling:

  } catch (e) {
-   console.log({ e });
+   console.error('Error updating sheet actions:', e);
+   throw e;
  }
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
console.log({ e });
}
console.error('Error updating sheet actions:', e);
throw e;
}

Comment on lines +13 to +22
sheets.forEach(async (sheet) => {
const loadAllFields = await getAllFields(sheets, sheet.slug);
setSheetActions(sheet, [
copyColumnValuesBlueprint(loadAllFields),
...(sheet.config.actions?.filter(
(a) =>
a.operation !== 'copy-column'
) || []),
]);
})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Use 'for...of' loop instead of 'forEach' with async callbacks

Using Array.forEach with an async callback does not wait for the promises to resolve, which can lead to unexpected behavior. It's recommended to use a for...of loop to handle asynchronous operations sequentially.

Apply this diff to fix the issue:

- sheets.forEach(async (sheet) => {
+ for (const sheet of sheets) {
+   const loadAllFields = await getAllFields(sheets, sheet.slug);
+   await setSheetActions(sheet, [
+     copyColumnValuesBlueprint(loadAllFields),
+     ...(sheet.config.actions?.filter(
+       (a) =>
+         a.operation !== 'copy-column'
+     ) || []),
+   ]);
+ }
-   const loadAllFields = await getAllFields(sheets, sheet.slug);
-   setSheetActions(sheet, [
-     copyColumnValuesBlueprint(loadAllFields),
-     ...(sheet.config.actions?.filter(
-       (a) =>
-         a.operation !== 'copy-column'
-     ) || []),
-   ]);
- })
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
sheets.forEach(async (sheet) => {
const loadAllFields = await getAllFields(sheets, sheet.slug);
setSheetActions(sheet, [
copyColumnValuesBlueprint(loadAllFields),
...(sheet.config.actions?.filter(
(a) =>
a.operation !== 'copy-column'
) || []),
]);
})
for (const sheet of sheets) {
const loadAllFields = await getAllFields(sheets, sheet.slug);
await setSheetActions(sheet, [
copyColumnValuesBlueprint(loadAllFields),
...(sheet.config.actions?.filter(
(a) =>
a.operation !== 'copy-column'
) || []),
]);
}

Comment on lines +28 to +30
const { key_copy_from, key_paste_to } = job.input
const patch = records.map(copyColumnValues(key_copy_from, key_paste_to))

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue

Validate input keys before copying column values

There is no validation to ensure that key_copy_from and key_paste_to are valid and not the same. Adding validation will prevent potential errors and unintended data overwrites.

Apply this diff to add validation:

  const { key_copy_from, key_paste_to } = job.input
+ if (!key_copy_from || !key_paste_to || key_copy_from === key_paste_to) {
+   throw new Error('Invalid input: Source and destination columns must be specified and cannot be the same.')
+ }
  const patch = records.map(copyColumnValues(key_copy_from, key_paste_to))
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
const { key_copy_from, key_paste_to } = job.input
const patch = records.map(copyColumnValues(key_copy_from, key_paste_to))
const { key_copy_from, key_paste_to } = job.input
if (!key_copy_from || !key_paste_to || key_copy_from === key_paste_to) {
throw new Error('Invalid input: Source and destination columns must be specified and cannot be the same.')
}
const patch = records.map(copyColumnValues(key_copy_from, key_paste_to))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants