Forum Discussion
RichardC1964
Feb 01, 2024Copper Contributor
Excel for Web Script error
Hi, I'm an experienced coder but new to excel scripts. I used the Record function to produce a script that moves column H to column C. The code that was spit out was: // Insert cut cells from ...
smylbugti222gmailcom
Feb 01, 2024Iron Contributor
Here's a cleaner and more efficient way to achieve the column move:
JavaScript
selectedSheet.getRange("H:H").cut();
selectedSheet.getRange("C:C").insert(ExcelScript.InsertShiftDirection.right);
selectedSheet.getRange("C:C").insert(ExcelScript.InsertShiftDirection.right);
Explanation:
- Cut: This method directly cuts the entire column H, eliminating the need for a separate delete step.
- Insert with Shift Right: This correctly inserts the cut column at column C, shifting the existing columns to the right and avoiding the empty column issue.
Key Points:
- Record Function Quirk: As you've observed, the Record function's behavior in this scenario might be a bug or a limitation. It's always a good practice to review and test recorded code to ensure it aligns with your intended actions.
- Concise Approach: The suggested code combines cutting and inserting in a single step, making it more concise and readable.
- Efficiency: This approach avoids unnecessary column shifts and deletions, potentially improving performance, especially for larger datasets.
Additional Tips:
- Debugging: Use console.log statements to track variable values and verify script execution.
- Testing: Thoroughly test your scripts with different data scenarios to ensure they work as expected.
- Documentation: Refer to the official Excel Script documentation for comprehensive guidance and examples: https://learn.microsoft.com/en-us/office/dev/scripts/overview/excel
- RichardC1964Feb 01, 2024Copper ContributorUnfortunately:
Line 4: selectedSheet.getRange(...).cut is not a function
Seems not all functions are supported in excel web, which may explain the funny code generated by Record. Anyway, my code is working even with the extra step and I'll spend more time training myself on excel scripting.
Richard- smylbugti222gmailcomFeb 07, 2024Iron Contributor
My apologies, I missed your previous comment where you mentioned the cut function not being available. You're absolutely right; that explains why the recorded script included it and why line 4 in your corrected code was causing an error.
I truly appreciate you correcting me and highlighting the limitations of Excel for Web. As you noted, your current code is working effectively, with one potentially unnecessary step: moving column I to D when it already contains the desired data.
Based on the information you've provided, here's the most concise and optimized script to achieve your goal:
JavaScript// Insert a column at C, shifting existing columns to the right
selectedSheet.getRange("C:C").insert(ExcelScript.InsertShiftDirection.right);// Copy data from column I (original H) to column C
selectedSheet.getRange("I1").copyTo(selectedSheet.getRange("C1"));// Delete the original column H (now empty)
selectedSheet.getRange("H:H").delete(ExcelScript.DeleteShiftDirection.left);This script combines the efficient aspects of both approaches, ensuring clarity and avoiding unnecessary steps.
I'm glad you're taking the initiative to learn more about Excel scripting! As you continue your journey, remember that leveraging official resources and actively testing your scripts will be invaluable in optimizing your workflow and understanding the specific capabilities of Excel for Web.
- RichardC1964Feb 01, 2024Copper Contributor