1

我有以下情況。 公式電子表格中的某些列應該按順序進行復制,而不會將舊內容覆蓋到主電子表格文件中。將列值複製到另一個谷歌電子表格文件而不覆蓋

我設法下面的腳本

function copy() { 

var ss = SpreadsheetApp.getActive(), 
    target =ss.getSheetByName('Sheet2'), //fill in the name of the sheet 
    lastColumn = target.getLastColumn(), 
    sourceVal = ss.getSheetByName('Sheet1').getRange("C1:C58") 
     .getValues(); 
target.getRange(1, lastColumn + 1, sourceVal.length, sourceVal[0].length) 
    .setValues(sourceVal); 

var ss = SpreadsheetApp.getActive(), 
    target =ss.getSheetByName('Sheet2'), //fill in the name of the sheet 
    lastColumn = target.getLastColumn(), 
    sourceVal = ss.getSheetByName('Sheet1').getRange("H1:H58") 
     .getValues(); 
target.getRange(1, lastColumn + 1, sourceVal.length, sourceVal[0].length) 
    .setValues(sourceVal); 
} 

這會爲需要的列的工作量,它不會覆蓋舊的信息結合起來。

不幸的是,我不明白如何使這個工作與其他電子表格文件。

最終結果應該從10個文件中讀取,每個文件2列(列不是連續的)讀入最終的主文件。

在此先感謝您抽出時間。

回答

0

這個工作對我來說:

function copy() { 
    var sources = [ 
    {id: '1XTdXQjcy48D63Yl-fHFOvCocZo5jWN10d_fYfhf5umo', // ID of first source file 
    sheet1: 'Sheet1', 
    range1: 'C1:C58', 
    sheet2: 'Sheet1', 
    range2: 'H1:H58' 
    }, 
    {id: '12wpXOlkKI2Zb8n5op4xuWQrWn7fyiH8j49NV8kB2b9s', 
    sheet1: 'Sheet1', 
    range1: 'C1:C58', 
    sheet2: 'Sheet1', 
    range2: 'H1:H58' 
    } // add next sheets 
    ] 
    sources.forEach(function(val) { 
    val.val1 = SpreadsheetApp.openById(val.id).getSheetByName(val.sheet1).getRange(val.range1).getValues(); 
    val.val2 = SpreadsheetApp.openById(val.id).getSheetByName(val.sheet2).getRange(val.range2).getValues(); 
    return val 
    }) 

    var target = SpreadsheetApp.getActive().getSheetByName('Sheet2') 

    for (var i=0; i<sources.length; i++) { 
    var lastColumn = target.getLastColumn() 
    target.getRange(1, lastColumn + 1, sources[i].val1.length, sources[i].val1[0].length).setValues(sources[i].val1); 

    var lastColumn = target.getLastColumn() 
    target.getRange(1, lastColumn + 1, sources[i].val2.length, sources[i].val2[0].length).setValues(sources[i].val2); 
    } 
} 

記住要改變標識的電子表格[1],表和範圍的名稱。使用openById(ID)

+1

這太棒了。 我理解它(不知何故),我可以根據要求進行更改。 非常感謝。 –

0

嘗試:

  • 打開具有給定ID的電子表格。

代碼示例:

// The code below opens a spreadsheet using its ID and logs the name for it. 
// Note that the spreadsheet is NOT physically opened on the client side. 
// It is opened on the server only (for modification by the script). 
var ss = SpreadsheetApp.openById("abc1234567"); 
Logger.log(ss.getName()); 

對於您訪問不同的表,並將其插入到主文件使用openById(id)

希望這會有所幫助。

相關問題