Google 应用程序脚本:从 A 列获取所有 URL,并分别在 master 中打开 openByURL、getSheetByName、getRange、getValues 和 setValues sheet
Google App Script: Fetch all URLs from column A, and individually openByURL,getSheetByName, getRange, getValues, then setValues in master sheet
我对 Google 应用程序脚本很陌生,但在从单个 Google Sheet 中检索值方面取得了一些成功,但不知道如何修改代码以是动态的,而是从列中获取 URLs。
我将如何循环此函数以遍历目标 sheet 中的 Google Sheet URLS 列表,所以我不不必手动添加每个 URL?
这是我当前的代码,非常适合单个 sheet。
function copypaste(sourcelink,sourcesheet,sourcerange,destilink,destisheet,destirange) {
//Source Link
var ssraw = SpreadsheetApp.openByUrl(sourcelink);
var sheetraw = ssraw.getSheetByName(sourcesheet);
var range = sheetraw.getRange(sourcerange);
var data = range.getValues();
//Destination
var ss = SpreadsheetApp.openByUrl(destilink);
var sheet = ss.getSheetByName(destisheet);
//Transfer to destination range
sheet.getRange(destirange).clearContent();
sheet.getRange(destirange).setValues(data);
}
// This function will add rows to destination sheet to ensure they match the source, to avoid any errors.
function startImport() {
//Calculate number of rows in source
var object = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/#1/edit").getSheetByName("Sheet1").getRange("A2:B").getValues()
var rownumber = object.length
//Calculate number of rows in destination
var object2 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/#2/edit").getSheetByName("Sheet2").getRange("A1:B").getValues()
var rownumber2 = object2.length
//Add rows based on differences
var addrows = rownumber - rownumber2
if(addrows>0){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("[DATA PULL] Roster Offshore").insertRowsAfter(rownumber2,addrows)
}
//Refreshes sheet, so the next lines of code do not give an error that the rows are still not the same.
SpreadsheetApp.flush()
//Imports the data into destination.
copypaste("https://docs.google.com/spreadsheets/d/#1/edit",
"Sheet1",
"A2:B",
"https://docs.google.com/spreadsheets/d/#2/edit",
"Sheet2",
"A1:B")
}
要从电子表格中获取电子表格 URL 列表,您可以使用以下代码片段:
const spreadsheetId = "###"; // Please set the source Spreadsheet ID.
const ss = SpreadsheetApp.openById(spreadsheetId);
const sheetName = "xxxx"; // Please enter the sheet name you are grabbing data from
const sheet = ss.getSheetByName(sheetName);
const rangeA1 = "A7:B11"; // Please modify this range accordingly to where the values are stored
const range = sheet.getRange(rangeA1);
const vals = range.getValues();
const finalListOfURLs = vals.flat(); // In case your URLs span multiple columns, this will flatten it into a 1D array.
console.log(finalListOfURLs);
如果这满足您的需要,请告诉我! (这将专门为您提供 URL 列表作为数组,仅此而已。)
我对 Google 应用程序脚本很陌生,但在从单个 Google Sheet 中检索值方面取得了一些成功,但不知道如何修改代码以是动态的,而是从列中获取 URLs。
我将如何循环此函数以遍历目标 sheet 中的 Google Sheet URLS 列表,所以我不不必手动添加每个 URL?
这是我当前的代码,非常适合单个 sheet。
function copypaste(sourcelink,sourcesheet,sourcerange,destilink,destisheet,destirange) {
//Source Link
var ssraw = SpreadsheetApp.openByUrl(sourcelink);
var sheetraw = ssraw.getSheetByName(sourcesheet);
var range = sheetraw.getRange(sourcerange);
var data = range.getValues();
//Destination
var ss = SpreadsheetApp.openByUrl(destilink);
var sheet = ss.getSheetByName(destisheet);
//Transfer to destination range
sheet.getRange(destirange).clearContent();
sheet.getRange(destirange).setValues(data);
}
// This function will add rows to destination sheet to ensure they match the source, to avoid any errors.
function startImport() {
//Calculate number of rows in source
var object = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/#1/edit").getSheetByName("Sheet1").getRange("A2:B").getValues()
var rownumber = object.length
//Calculate number of rows in destination
var object2 = SpreadsheetApp.openByUrl("https://docs.google.com/spreadsheets/d/#2/edit").getSheetByName("Sheet2").getRange("A1:B").getValues()
var rownumber2 = object2.length
//Add rows based on differences
var addrows = rownumber - rownumber2
if(addrows>0){
SpreadsheetApp.getActiveSpreadsheet().getSheetByName("[DATA PULL] Roster Offshore").insertRowsAfter(rownumber2,addrows)
}
//Refreshes sheet, so the next lines of code do not give an error that the rows are still not the same.
SpreadsheetApp.flush()
//Imports the data into destination.
copypaste("https://docs.google.com/spreadsheets/d/#1/edit",
"Sheet1",
"A2:B",
"https://docs.google.com/spreadsheets/d/#2/edit",
"Sheet2",
"A1:B")
}
要从电子表格中获取电子表格 URL 列表,您可以使用以下代码片段:
const spreadsheetId = "###"; // Please set the source Spreadsheet ID.
const ss = SpreadsheetApp.openById(spreadsheetId);
const sheetName = "xxxx"; // Please enter the sheet name you are grabbing data from
const sheet = ss.getSheetByName(sheetName);
const rangeA1 = "A7:B11"; // Please modify this range accordingly to where the values are stored
const range = sheet.getRange(rangeA1);
const vals = range.getValues();
const finalListOfURLs = vals.flat(); // In case your URLs span multiple columns, this will flatten it into a 1D array.
console.log(finalListOfURLs);
如果这满足您的需要,请告诉我! (这将专门为您提供 URL 列表作为数组,仅此而已。)