Google apps script Google工作表未将行复制到正确的工作表
我使用电子邮件地址作为唯一标识符,将一张数据表拆分为多张数据表 我使用的是我找到并编辑过的脚本的变体,但使用的是copyTo而不是appendRow 问题在于,它似乎将特定数据集的最后一行复制到后续工作表中Google apps script Google工作表未将行复制到正确的工作表,google-apps-script,google-sheets,copyto,Google Apps Script,Google Sheets,Copyto,我使用电子邮件地址作为唯一标识符,将一张数据表拆分为多张数据表 我使用的是我找到并编辑过的脚本的变体,但使用的是copyTo而不是appendRow 问题在于,它似乎将特定数据集的最后一行复制到后续工作表中 function myFunction() { var theWorkbook = SpreadsheetApp.getActiveSpreadsheet(); var theSheet = theWorkbook.getSheetByName("Copied&q
function myFunction() {
var theWorkbook = SpreadsheetApp.getActiveSpreadsheet();
var theSheet = theWorkbook.getSheetByName("Copied");
// This var will contain all the values from column B -> The email addresses
var theEmails = theSheet.getRange("B:B").getValues();
// This var will contain all the rows
var rows = theSheet.getDataRange().getValues();
//Set the first row as the header
var header = rows[0];
//Store the Sheets already created
var completedSheets = []
//The last created Sheet
var last = theEmails[1][0]
for (var i = 1; i < theEmails.length; i++) {
//Check if the Sheet is already done, if not create the sheet
if(!completedSheets.includes(theEmails[i][0])) {
//Set the Sheet name = email address (except if there is no name, then = Blank)
if (theEmails[i][0] === "") {
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet("Blank");
} else {
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(theEmails[i][0]);
}
//append the header
currentSheet.appendRow(header);
var rowtocopy = theSheet.getRange(i,1,1,8);
rowtocopy.copyTo(currentSheet.getRange(currentSheet.getLastRow()+1, 1));
completedSheets.push(theEmails[i][0])
last = theEmails[i][0]
} else if (last == theEmails[i][0]) {
// If the sheet is created append the row to the sheet
var currentSheet = SpreadsheetApp.getActiveSpreadsheet();
var activespreadsheet = SpreadsheetApp.getActiveSheet();
var rowtocopy = theSheet.getRange(i,1,1,8);
rowtocopy.copyTo(activespreadsheet.getRange(activespreadsheet.getLastRow()+1, 1));
}
}
}
我想这可能是因为我使用的是getActiveSheet,而不是明确地针对正确的工作表,所以我尝试过这样做,使用类似于:getSheetByName(emailAddress[I][0])的东西
然而,这一直是空的。当我记录emailaddress的值时,在记录器中查看,这些值显示正确,因此不太确定为什么会变为null
以下是“有效”的脚本,但将最后一行复制到后续工作表
function myFunction() {
var theWorkbook = SpreadsheetApp.getActiveSpreadsheet();
var theSheet = theWorkbook.getSheetByName("Copied");
// This var will contain all the values from column B -> The email addresses
var theEmails = theSheet.getRange("B:B").getValues();
// This var will contain all the rows
var rows = theSheet.getDataRange().getValues();
//Set the first row as the header
var header = rows[0];
//Store the Sheets already created
var completedSheets = []
//The last created Sheet
var last = theEmails[1][0]
for (var i = 1; i < theEmails.length; i++) {
//Check if the Sheet is already done, if not create the sheet
if(!completedSheets.includes(theEmails[i][0])) {
//Set the Sheet name = email address (except if there is no name, then = Blank)
if (theEmails[i][0] === "") {
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet("Blank");
} else {
var currentSheet = SpreadsheetApp.getActiveSpreadsheet().insertSheet(theEmails[i][0]);
}
//append the header
currentSheet.appendRow(header);
var rowtocopy = theSheet.getRange(i,1,1,8);
rowtocopy.copyTo(currentSheet.getRange(currentSheet.getLastRow()+1, 1));
completedSheets.push(theEmails[i][0])
last = theEmails[i][0]
} else if (last == theEmails[i][0]) {
// If the sheet is created append the row to the sheet
var currentSheet = SpreadsheetApp.getActiveSpreadsheet();
var activespreadsheet = SpreadsheetApp.getActiveSheet();
var rowtocopy = theSheet.getRange(i,1,1,8);
rowtocopy.copyTo(activespreadsheet.getRange(activespreadsheet.getLastRow()+1, 1));
}
}
}
我做错了什么?这是一个经典的表单,它的值数组是基于索引零的,API的“行”和“列”是基于索引一的 在
for
循环中,对getRange(row,…)
调用中的值数组和API“row”参数使用相同的索引i
。任何使用床单的人都会对自己这样做,可能已经很多次了
更新您的位置
var rowtopy=theSheet.getRange(i,1,1,8);
到
var rowtopy=sheet.getRange(i+1,1,1,8);
它会起作用的