所以我是新來的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);
}
}
謝謝埃裏克。似乎有意義的是,您在製作腳本的電子表格上提供此映射列表。然後,我們打開源表單,並按部門提取數據,然後放在目標表單上......我想我需要有人來幫助我編寫這個表單...開始對我變得很有幫助。 – user1393960