如何解决复制一个工作表中的选定列并将它们添加到另一工作表中的选定列
我想创建一个简单的谷歌应用程序脚本来将特定列复制到另一个工作表中。 以前我尝试过使用 getLastRow 但我坚持修改它。
var destinationSheetLastRow = destinationSheet.getDatarange().getLastRow();
这是我的电子表格:https://docs.google.com/spreadsheets/d/1rGvmlKCmbjDSCLCC2Kujft5e4ngbSLzJd2NYu0sxISs/edit?usp=sharing
这是目前修改后的脚本:
function pastemulticol(sourceSheet,destinationSheet,sourceColumns,destinationColumns,doneColumn){
var sourceDatarange = sourceSheet.getDatarange();
var sourceDataValues = sourceDatarange.getValues();
var sourcesheetFirstRow = 0;
var sourceSheetLastRow = sourceDatarange.getLastRow();
var destinationSheetLastRow = destinationSheet.getDatarange().getLastRow();
var pendingCount = 0;
//Find the row start for copying
for(i = 0; i < sourceDataValues.length; i++){
if(sourceDataValues[i][doneColumn-1] === "copied"){
sourcesheetFirstRow++;
};
if(sourceDataValues[i][doneColumn-1] === ""){
pendingCount++;
};
};
//Update Source sheet first row to take into account the header
var header = sourceSheetLastRow-(sourcesheetFirstRow + pendingCount);
sourcesheetFirstRow = sourcesheetFirstRow+header;
// if the first row equals the last row then there is no data to paste.
if(sourcesheetFirstRow === sourceSheetLastRow){return};
var sourceSheetRowLength = sourceSheetLastRow - sourcesheetFirstRow;
//Iterate through each column
for(i = 0; i < destinationColumns.length; i++){
var destinationRange = destinationSheet.getRange(destinationSheetLastRow+1,destinationColumns[i],sourceSheetRowLength,1);
var sourceValues = sourceDataValues.slice(sourcesheetFirstRow-1,sourceSheetLastRow);
var columnValues =[]
for(j = header; j < sourceValues.length; j++){
columnValues.push([sourceValues[j][sourceColumns[i]-1]]);
};
destinationRange.setValues(columnValues);
};
//Change Source Sheet to copied.
var copiedArray =[];
for(i=0; i<sourceSheetRowLength; i++){copiedArray.push(["copied"])};
var copiedRange = sourceSheet.getRange(sourcesheetFirstRow+1,doneColumn,1)
copiedRange.setValues(copiedArray);
};
function runsies(){
var ss = SpreadsheetApp.openById("1snMyf8YZZ0cGlbMIvZY-fAXrI_dJpPbl7rKcYCkPDpk");
var source = ss.getSheetByName("Source");
var destination = ss.getSheetByName("Destination");
var sourceCols = [4,5,6,7,8,9,10];
var destinationCols = [7,10,11,12,13];
var doneCol = 12
//Run our copy and append function
pastemulticol(source,destination,sourceCols,destinationCols,doneCol);
};
解决方法
您的代码取自我在博客文章 Copy Selected Columns in One Sheet and Add Them To The Bottom of Different Selected Columns in Another Sheet 中的教程,只需稍作调整即可。
我认为问题可能在于您的“目的地”工作表标签的其他列中有一堆公式。因此,考虑到包括其他公式在内的所有数据,获取工作表的最后一行将导致获取最后一行。
您可能会在我写的后续博客文章中发现此解释很有帮助:Google Apps Script: Get the last row of a data range when other columns have content like hidden formulas and check boxes
简而言之,您可以将 destinationSheetLastRow 变量更改为类似这样的简单内容。
var destinationSheetLastRow = (()=>{
let destinationSheetFirstRow = 7; // The first row of data after your header.
//Get a sample range to find the last value in the paste columns.
let sampleRange = destinationSheet.getRange(destinationSheetFirstRow,destinationColumns[0],destinationSheet.getLastRow())
.getValues();
let sampleLastRow = 0;
while(sampleLastRow < sampleRange.length){
if (sampleRange[sampleLastRow][0] == ""){
break;
}
sampleLastRow++;
};
return sampleLastRow;
})()
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。