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

feat(formula): add register async function #4399

Merged
merged 8 commits into from
Jan 2, 2025
Merged

Conversation

hexf00
Copy link
Member

@hexf00 hexf00 commented Dec 31, 2024

close #xxx

univerAPI.getFormula().registerFunction('HELLO', (name) => `Hello, ${name}!`, 'A simple greeting function');
// Use the function in a cell
univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue('World');
univerAPI.getActiveWorkbook().getActiveSheet().getRange('A2').setValue({ f: '=HELLO(A1)' });
// A2 will display: "Hello, World!"

// Mock a user score fetching function

 univerAPI.getFormula().registerAsyncFunction('FETCH_USER_SCORE',
   async (userId) => {
     await new Promise(resolve => setTimeout(resolve, 1000));
     // Mock fetching user score from database
     return userId * 10 + Math.floor(Math.random() * 20);
   },
   {
     description: 'customFunction.description.FETCH_USER_SCORE',
     locales: {
       'zhCN': {
          'customFunction': {
              'description': {
                  'FETCH_USER_SCORE': '从数据库中获取用户分数'
              }
          }
       },
       'enUS': {
          'customFunction': {
              'description': {
                  'FETCH_USER_SCORE': 'Mock fetching user score from database'
              }
          }
       }
     }
   }
 );

 // Use in cell
 univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue({ f: '=FETCH_USER_SCORE(42)' });
 // After 1 second, A1 will display a score

Pull Request Checklist

  • Related tickets or issues have been linked in the PR description (or missing issue).
  • Naming convention is followed (do please check it especially when you created new plugins, commands and resources).
  • Unit tests have been added for the changes (if applicable).
  • Breaking changes have been documented (or no breaking changes introduced in this PR).

Copy link

github-actions bot commented Dec 31, 2024

View Deployment

📑 Examples 📚 Storybook
🔗 Preview link 🔗 Preview link

Copy link

github-actions bot commented Dec 31, 2024

Playwright test results

passed  21 passed

Details

stats  21 tests across 10 suites
duration  4 minutes, 46 seconds
commit  819d078
info  For more information, see full report

Copy link

codecov bot commented Dec 31, 2024

Codecov Report

Attention: Patch coverage is 15.05376% with 79 lines in your changes missing coverage. Please review.

Project coverage is 33.28%. Comparing base (407f68b) to head (819d078).
Report is 1 commits behind head on dev.

Files with missing lines Patch % Lines
packages/sheets-formula/src/facade/f-formula.ts 0.00% 25 Missing ⚠️
...-formula/src/services/register-function.service.ts 0.00% 24 Missing ⚠️
...ngine-formula/src/engine/ast-node/function-node.ts 48.27% 15 Missing ⚠️
...ervices/remote/remote-register-function.service.ts 0.00% 9 Missing ⚠️
packages/engine-formula/src/facade/f-formula.ts 0.00% 3 Missing ⚠️
...es/engine-formula/src/functions/custom-function.ts 0.00% 2 Missing ⚠️
...ages/engine-formula/src/functions/base-function.ts 0.00% 1 Missing ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##              dev    #4399   +/-   ##
=======================================
  Coverage   33.28%   33.28%           
=======================================
  Files        2533     2533           
  Lines      130776   130854   +78     
  Branches    29170    29185   +15     
=======================================
+ Hits        43525    43557   +32     
- Misses      87251    87297   +46     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@hexf00 hexf00 force-pushed the feat/async-promise-function branch from cbc13e0 to 722fefe Compare January 2, 2025 06:21
@univer-bot
Copy link

univer-bot bot commented Jan 2, 2025

Bot detected the issue body's language is not English, translate it automatically. 👯👭🏻🧑‍🤝‍🧑👫🧑🏿‍🤝‍🧑🏻👩🏾‍🤝‍👨🏿👬🏿

Origin Title: feat(formula): add register async function

Title: feat(formula): add register async function


close #xxx

univerAPI.getFormula().registerFunction('HELLO', (name) => `Hello, ${name}!`, 'A simple greeting function');
// Use the function in a cell
univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue('World');
univerAPI.getActiveWorkbook().getActiveSheet().getRange('A2').setValue({ f: '=HELLO(A1)' });
// A2 will display: "Hello, World!"

// Mock a user score fetching function

 univerAPI.getFormula().registerAsyncFunction('FETCH_USER_SCORE',
   async (userId) => {
     await new Promise(resolve => setTimeout(resolve, 1000));
     // Mock fetching user score from database
     return userId * 10 + Math.floor(Math.random() * 20);
   },
   {
     description: 'customFunction.description.FETCH_USER_SCORE',
     locales: {
       'zhCN': {
          'customFunction': {
              'description': {
                  'FETCH_USER_SCORE': '从数据库中获取用户分数'
              }
          }
       },
       'enUS': {
          'customFunction': {
              'description': {
                  'FETCH_USER_SCORE': 'Mock fetching user score from database'
              }
          }
       }
     }
   }
 );

 // Use in cell
 univerAPI.getActiveWorkbook().getActiveSheet().getRange('A1').setValue({ f: '=FETCH_USER_SCORE(42)' });
 // After 1 second, A1 will display a score

Pull Request Checklist

  • Related tickets or issues have been linked in the PR description (or missing issue).
  • Naming convention is followed (do please check it especially when you created new plugins, commands and resources).
  • Unit tests have been added for the changes (if applicable).
  • Breaking changes have been documented (or no breaking changes introduced in this PR).

@hexf00 hexf00 requested a review from DR-Univer January 2, 2025 10:46
@DR-Univer
Copy link
Collaborator

Good job

@hexf00 hexf00 merged commit c8d287b into dev Jan 2, 2025
9 checks passed
@hexf00 hexf00 deleted the feat/async-promise-function branch January 2, 2025 16:02
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