How to copy value from one column to another on Google Sheets?

我是研究僧i 提交于 2021-02-04 08:32:14

问题


Is there any formula or script to copy only values (not formulas) from one column to another?. I don't want to use CTRL + SHIFT + V because I need this to be automatic. For example:

Column A  Column B
Value1
Value2
Value3

All the values of column A are calculated with an array formula, I need that everytime that column A has a new record the value passes to column B for ever, so if the value or formula in column A is deleted the copied value remains in column B, is it possible to do this?

Please any help!


回答1:


You want to

  • copy values from one column on a google sheet to another column on the same google sheet

If my understanding is correct, how about this answer? Please consider this as one of many possible ways of solving your problem.

Flow:
1. Get active sheet 2. Get active range (column) 3. Paste values into the range (column)

Sample Script:

function copyvalues() {
  const ss = SpreadsheetApp.getActive();
  const sh = ss.getSheetByName('Sheet1');
  const rg = sh.getRange(1,1,sh.getLastRow()); //Explore the various getRange functions
  const va = rg.getValues();  //Explore Arrays if you are not familiar with it
  va.forEach(function(r,i){
    sh.getRange(i+1,2).setValues(r[0]).setNumberFormat('#,##0.00');  //Use setNumberFormat if you need to format your values with two decimal places and thousands separator (if not already done so)
  })
}

Reference:

  • Difference between const and var
  • getValues()
  • Google App Script Best Practices - see using batch operations using Arrays


来源:https://stackoverflow.com/questions/61532891/how-to-copy-value-from-one-column-to-another-on-google-sheets

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!