Jan 31 2024 09:16 PM
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:
This is wrong as the insert at C move H to I. This means G is actually copied. Also, an empty column is left over so its not really cut and insert/paste as advertised. So maybe the Record function is bugged? It seems the simplest of all possible scripts for it to produce...
My corrected code is
Jan 31 2024 11:00 PM
Here's a cleaner and more efficient way to achieve the column move:
Explanation:
Key Points:
Additional Tips:
Feb 01 2024 06:54 AM
Feb 07 2024 03:34 AM
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:
// 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.