所以我是 Javascript、谷歌表格和这个网站的新手。但我想我会分享一些你可能会感兴趣的东西。这是我制作的一个脚本,用于根据月份将数据从工作表移动到各种目标工作表中。
它的工作原理是逐个遍历targetSheetNames
,查看某个数据的月份值是否与数组中工作表名称的索引号匹配。
所以在这种情况下,数组从 0-11 开始,这很方便,因为 Javascript 中的月份也是 0-11。
出于您的目的,您可能需要添加更多位,以检查它是否是正确的数据等。但它为您提供了一个在同一个电子表格中将数据移动到多个工作表的示例。
function splitByMonth(){
var targetSheetNames = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]; // Sheet names, in month order. Order is important. Name is not. Sheets must exist. This will not create them.
var sourceSheet = "Archive"; // Name of sheet with data in it.
var dateColumn = 1; // Column number with date in it. A = 1, B = 2, C = 3, etc
var ss, sheet, values, valuesLength, counter, archive = [];
for (var i = 0; i < targetSheetNames.length; i++){
ss = SpreadsheetApp.getActiveSpreadsheet();
sheet = ss.getSheetByName(sourceSheet);
values = sheet.getDataRange().getValues();
valuesLength = values.length;
counter = 1; // This starts at one because it assumes a header row.
archive = [];
while (counter < values.length){
var d = new Date(values[counter][dateColumn - 1]);
var m = d.getMonth();
if (m == i ){
archive.push(values.splice(counter, 1)[0]);
}
else
{
counter++;
}
}
var archiveLength = archive.length;
if (!archiveLength) {continue;}
var dataSs = SpreadsheetApp.getActiveSpreadsheet();
var targetSheet = dataSs.getSheetByName(sourceSheetKeys[i]);
var lastRow = targetSheet.getLastRow();
var requiredRows = lastRow + archiveLength - targetSheet.getMaxRows();
if (requiredRows > 0) targetSheet.insertRowsAfter(lastRow, requiredRows);
targetSheet.getRange(lastRow + 1, 1, archiveLength, archive[0].length).setValues(archive);
}
}