是否有一种简单的格式来通过脚本移动数据列? [英] Is there an easy format to move columns of data by script?

查看:75
本文介绍了是否有一种简单的格式来通过脚本移动数据列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从一张纸上移动几行到另一张,但是我看到的所有脚本都没有看到解决这个问题。

I want to move several rows from one sheet to another but all the scripts that I am looking at don't see to address this.

例如,我想将Sheet1上的A列,B,C列移动到Sheet2上的G列,I,L列。

For example, I want to move Column A,B,C on Sheet1 to Column G,I,L on Sheet2.

可以这样做吗?

谢谢。

推荐答案

这可以用简单的代码完成:

This could be done with simple code:

function moveCols() {
var ss = SpreadsheetApp.getActive();
var sourceSheet = ss.getSheetByName('Sheet1');
var destSheet = ss.getSheetByName('Sheet2');
sourceSheet.getRange('A:A').moveTo(destSheet.getRange('G1'))
sourceSheet.getRange('B:B').moveTo(destSheet.getRange('I1'))
sourceSheet.getRange('C:C').moveTo(destSheet.getRange('L1'))
}

如果您只想复制数据(而不是移动它),请将.moveTo()方法更改为copyTo()。

If you only want to copy the data (and not move it) change the .moveTo() method to copyTo().

编辑:如果您需要将列移动到另一个电子表格,请尝试:

If you need to move the columns to another spreadsheet, try:

function moveCols() {
var sourceSheet = SpreadsheetApp.getActive()
    .getSheetByName('Sheet1'),
    destSheet = SpreadsheetApp.openById('ID_OF_DESTINATION_SHEET')
        .getSheetByName('Sheet2'),
    sourceRanges = ['A:A', 'B:B', 'C:C'],
    targetRanges = [7, 9, 12]
        .map(function (r, i) {
            var sr = sourceSheet.getRange(sourceRanges[i]),
                val = sr.getValues();
            destSheet.getRange(1, r, val.length, val[0].length)
                .setValues(val);
            sr.clear()
        });
}

这篇关于是否有一种简单的格式来通过脚本移动数据列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆