在 Google 个工作表中将多个函数合并到一个脚本中
Combine multiple functions in one script in Google sheets
我正在使用代码从多张工作表中提取数据。代码运行良好,但我需要从多个位置提取数据。我想一次把它全部拉下来。下面的代码可以很好地提取 ImportFALLOBS 数据(第一个函数),但不会执行 ImportSPRINGOBS 函数。
const ids = [
'1-PzUz2dlsLwA7lcndyWUZk4olgccE31jje8_JakZxXQ'
]
function ImportFALLOBS() {
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName('FALL OBS').getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FALLOBSIMPORT')
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result)
}
function ImportSPRINGOBS() {
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName('SPRING OBS').getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SPRINGOBSIMPORT')
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result)
}
您可以通过组合通用部分并更改 sheet 名称来使您的代码更小更快:
function ImportOBS() {
const sheetNames = ['FALL OBS','SPRING OBS'];
const importNames = ['FALLOBSIMPORT', 'SPRINGOBSIMPORT'];
sheetNames.forEach((sheetName,i)=>{
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName(sheetName).getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(importNames[i])
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result);
})
}
现在你只有一个功能:ImportOBS
我正在使用代码从多张工作表中提取数据。代码运行良好,但我需要从多个位置提取数据。我想一次把它全部拉下来。下面的代码可以很好地提取 ImportFALLOBS 数据(第一个函数),但不会执行 ImportSPRINGOBS 函数。
const ids = [
'1-PzUz2dlsLwA7lcndyWUZk4olgccE31jje8_JakZxXQ'
]
function ImportFALLOBS() {
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName('FALL OBS').getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('FALLOBSIMPORT')
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result)
}
function ImportSPRINGOBS() {
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName('SPRING OBS').getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('SPRINGOBSIMPORT')
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result)
}
您可以通过组合通用部分并更改 sheet 名称来使您的代码更小更快:
function ImportOBS() {
const sheetNames = ['FALL OBS','SPRING OBS'];
const importNames = ['FALLOBSIMPORT', 'SPRINGOBSIMPORT'];
sheetNames.forEach((sheetName,i)=>{
let result = []
ids.forEach((id, i) => {
let [headers, ...data] = SpreadsheetApp.openById(id).getSheetByName(sheetName).getRange(5,1,1,13).getValues()
if (i == 0) { result.push(headers.flat()) }
data.forEach(r => result.push(r))
})
var master = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(importNames[i])
master.clearContents()
master.getRange(2, 1, result.length, result[0].length).setValues(result);
})
}
现在你只有一个功能:ImportOBS