2013-06-25 113 views

回答

2

或許不會簡單,但我要說的更好的性能,得到一個大的範圍涵蓋你需要.get(Data)Range().getValues()的所有列,使用Javascript來剝離下來數組只有你所需要的列,並使用setValues()粘貼值一重擊:

function copyValuesOnly() { 
    var ss = SpreadsheetApp.getActiveSpreadsheet(); 
    var values = ss.getSheetByName('Source').getDataRange().getValues(); 
    values.shift(); //remove header row 
    var columns = [0, 2, 10, 29, 53]; //array of zero-based indices of required columns 
    var output = []; 
    for (var i = 0, length = values.length; i < length; i++) { 
    output[i] = []; 
    for (var j = 0, width = columns.length; j < width; j++) { 
     output[i][j] = values[i][columns[j]]; 
    } 
    } 
    ss.getSheetByName('Destination').getRange(2, 1, length, width).setValues(output); 
} 

,如果你需要複製格式和公式爲好,問題是在這種情況下最好的選擇可能是單獨的複製粘貼每一列,如你所說。

1

我的答案真的有點多餘/學術,因爲=QUERY()函數將允許你做你想要的。例如=QUERY(A1:D31,"Select C, A, B")我還給出了一個在示例表單上使用它的示例(鏈接如下)。 QUERY也可以與@AdamL的=IMPORTRANGE()explanation一起使用。我已經在我的函數中包含了這個功能來展示。最後,我的函數可以在電子表格中使用,或者在沒有修改的腳本中使用。在我的example spreadsheet上有使用QUERY(IMPORTRANGE())和我的功能copyColumns的示例。


我已經包含了一些驗證,以便該功能可以由較少的技術人員使用電子表格。希望對你也有用。我已經廣泛使用JS功能,包括RegExp,Array.mapConditional Operator,請在此處留言詢問需要的清晰度。

基本知識:它採用表格"SheetName!A,C,B"的字符串,其中SheetName!是可選的。它可以採用起始行,默認值爲1.它也可以通過給定sheetKey(帶或不帶起始行)來處理非本地電子表格。

例如:=copyCoumns("MyDataSheet!C,A,W",8)將複製列C,A和W次序與8排

開始這裏的功能!請享用!

function copyColumns(sourceRange,start,sheetKey) { 
    // Initialize optional parameter 
    if(!sheetKey && typeof start !== "number") { 
    sheetKey = start; 
    start = 1; 
    } else { 
    start = start || 1; 
    } 
    // Check SourceRange Input 
    var inputRe = /^((.*?!)(?=[a-z],?|[a-i][a-z]))?[a-i]?[a-z](,[a-i]?[a-z])*$/i; 
    if(!inputRe.test(sourceRange)) 
    throw "Invalid SourceRange: " + sourceRange; 

    // Check Start Row 
    if(typeof start !== "number") 
    throw "Starting row must be a number! Got: " + start; 
    if(start % 1 !== 0) 
    throw "Starting row must be an integer! Got: " + start; 
    if(start < 1) 
    throw "Starting row can't be less than 1! Got: " + start; 

    // Get the Source Sheet 
    try { 
    var ss = sheetKey 
      ? SpreadsheetApp.openById(sheetKey) 
      : SpreadsheetApp.getActiveSpreadsheet(); 
    } catch(err) { 
    throw "Problem getting sheet" + sheetKey + " - " + err; 
    } 
    var sheetName = sourceRange.match(/^.*?(?=!)/); 
    var sheet = sheetName 
      ? ss.getSheetByName(sheetName[0]) 
      : ss.getActiveSheet(); 

    // Check that everything is still valid 
    if(!sheet) 
    throw "Could not find sheet with name: " + sheetName; 
    if(start > sheet.getLastRow()) 
    throw "No data beyond row: " + start + " Last row: " + sheet.getLastRow(); 

    // Get the values 
    var lastCol = sheet.getLastColumn(); 
    var lastRow = sheet.getLastRow()-start+1; 
    var values = sheet.getRange(start,1,lastRow,lastCol).getValues(); 

    // Get the desired columns from the string 
    var desiredColMatch = sourceRange.match(/([a-i]?[a-z](,[a-i]?[a-z])*)$/i); 
    var desiredColumns = desiredColMatch[0].toUpperCase().split(","); 

    // In case the column we are trying to grab doesn't exist in the sheet 
    var lastColId = sheet.getMaxColumns() - 1; // Array is 0 indexed, Sheet is 1 

    // Get the numerical values of the passed in Column Ids 
    var columns = desiredColumns.map(function(colId){ 
    var num = colId.length - 1; // 0 or 1 
    var colNum = colId.charCodeAt(num)-65+num*26*(colId.charCodeAt(0)-64); 
    if(colNum > lastColId) 
     throw "Invalid Column: " + colId + " - Column not in: " + sheetName; 
    return colNum; 
    }); 

    //Map the values to a new array of just the columns we want 
    return values.map(function(row){ 
    return columns.map(function(col){ 
     return row[col] 
    }) 
    }); 
} 
相關問題