我有一个Google表格,其中包含约20列数据,所有数据均与视频文件有关。视频文件是足球比赛中的表演。视频文件位于Google驱动器中,并依次命名为m0001.mp4至m0124.mp4。假设该游戏我有124个视频文件(播放)。对于某些游戏,可能是m00356.mp4至m00495.mp4等。
我之前在excel中使用宏进行了此操作,并使用了第一个文件和文件名作为名称,它会自动显示与视频文件的超链接。
如何使用脚本执行相同的操作?
这是我来自excel的宏:
Sub nbsl()
Dim FldrLoc As String: FldrLoc = "m:\2011 season\nbsl\"
Dim rngAnchor As Range
Dim CurrentFile As String: CurrentFile = Dir(FldrLoc)
Do While CurrentFile <> vbNullString
Set rngAnchor = ActiveSheet.Range("V" & Rows.Count).End(xlUp).Offset(1, 0)
ActiveSheet.Hyperlinks.Add Anchor:=rngAnchor, _
Address:=FldrLoc & CurrentFile, _
TextToDisplay:=CurrentFile
CurrentFile = Dir()
Loop
End Sub
答案 0 :(得分:1)
感谢@Tednioz和@Niek Waarbroek,我能够弄清其中的大部分内容。
下面是我的代码。
function getFiles() {
var dApp = DriveApp;
var vfolder = dApp.getFoldersByName("video").next();
var yearfolder = vfolder.getFoldersByName("2018").next();
var gamefolder = yearfolder.getFoldersByName("game1").next();
var folderez = gamefolder.getFoldersByName("ez").next();
var foldersl = gamefolder.getFoldersByName("sl").next();
var ezIter = folderez.getFiles();
var slIter = foldersl.getFiles();
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(yearfolder+gamefolder);
var i = 2;
while (ezIter.hasNext()) {
var file = ezIter.next();
var name = file.getName();
var url = file.getUrl();
range = 'AC'+(i);
ss.getRange(range).setValue(url);
i++
}
i = 2;
while (slIter.hasNext()) {
var file = slIter.next();
var name = file.getName();
var url = file.getUrl();
range = 'AD'+(i);
ss.getRange(range).setValue(url);
i++
}
}
有些事情我还没弄清楚 1)如果您知道,有没有办法使用文件的完整路径? 示例:“ Driveroot:\ video \ 2018 \ game1 \ sl”
2)有没有一种方法可以使链接仅显示为文件名而不显示完整URL?
3)当您单击超链接时,是否可以自动播放视频并保持观看状态?
4)这些文件的名称将为dms0001,dms0002,....那不是它们被加载的顺序。有没有办法做到这一点?
答案 1 :(得分:0)
输出排序
此代码作为片段提供,以显示如何按名称顺序对结果进行排序。 所编写的代码分为两部分:第一部分在开始列AD的输出之前,对列AC中的结果进行排序。这不是绝对必要的,所有排序都可以合并
排序列AC
i++
}
// end of the first WHILE loop.
// START OF THE SORTING CODE-Column AC
// get the number of rows of content in Column AC
var ACvals = ss.getRange("AC2:AC").getValues();
var AClast = ACvals.filter(String).length;
// Logger.log("DEBUG: AClast = "+AClast); //DEBUG
// create a range
var range = sheet.getRange(2, 29, AClast);
// Logger.log("DEBUG: range = "+range.getA1Notation());//DEBUG
// sort the range by file name
range.sort({
column: 29,
ascending: true
});
// END OF THE SORTING CODE-Column AC
i = 2;
// start of the second WHILE loop
排序列AD
i++
}
// end of the second WHILE loop.
// START OF THE SORTING CODE-Column AD
// get the number of rows of content in Column AD
var ADvals = ss.getRange("AD2:AD").getValues();
var ADlast = ADvals.filter(String).length;
// Logger.log("DEBUG: ADlast = "+ADlast); //DEBUG
// create a range
var range = sheet.getRange(2, 30, ADlast);
// Logger.log("DEBUG: range = "+range.getA1Notation());//DEBUG
// sort the range by file name
range.sort({
column: 30,
ascending: true
});
// END OF THE SORTING CODE-Column AD
}
答案 2 :(得分:0)
我有两种有效的解决方案。我将同时在下面发布:
选项1)允许您为链接命名,而不使用文件名:
function getFiles() {
var dApp = DriveApp;
var vfolder = dApp.getFoldersByName("video").next();
var yearfolder = vfolder.getFoldersByName("2018").next();
var gamefolder = yearfolder.getFoldersByName("game2").next();
var folderez = gamefolder.getFoldersByName("ez").next();
var foldersl = gamefolder.getFoldersByName("sl").next();
var ezIter = folderez.getFiles();
var slIter = foldersl.getFiles();
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(yearfolder+gamefolder);
var i = 2;
var files = [];
while (ezIter.hasNext()) {
files.push(ezIter.next());
}
// sorts the files array by file names alphabetically
files = files.sort(function(a, b){
var aName = a.getName().toUpperCase();
var bName = b.getName().toUpperCase();
return aName.localeCompare(bName);
});
files.forEach(function(file){
var name = file.getName();
var url = file.getUrl();
range = 'AC'+(i);
var filename = 'ezvideo'+(i-1);
var semi = ";";
var hyp = "=hyperlink(\"";
var quot = "\"";
var clos = ")";
var title = hyp + url + quot + semi + quot + filename + quot + clos; // str='=hyperlink("' + f.getUrl() + '","' + f.getName() + '")';
ss.getRange(range).setValue(title);
i++;
});
var i = 2;
var files = [];
while (slIter.hasNext()) {
files.push(slIter.next());
}
// sorts the files array by file names alphabetically
files = files.sort(function(a, b){
var aName = a.getName().toUpperCase();
var bName = b.getName().toUpperCase();
return aName.localeCompare(bName);
});
files.forEach(function(file){
var name = file.getName();
var url = file.getUrl();
range = 'AD'+(i);
var filename = 'slvideo'+(i-1);
var semi = ";";
var hyp = "=hyperlink(\"";
var quot = "\"";
var clos = ")";
var title = hyp + url + quot + semi + quot + filename + quot + clos; // str='=hyperlink("' + f.getUrl() + '","' + f.getName() + '")';
ss.getRange(range).setValue(title);
i++;
});
}
或选项2;这要求您使用文件名进行排序:
function getFiles() {
var dApp = DriveApp;
var vfolder = dApp.getFoldersByName("video").next();
var yearfolder = vfolder.getFoldersByName("2018").next();
var gamefolder = yearfolder.getFoldersByName("game1").next();
var folderez = gamefolder.getFoldersByName("ez").next();
var foldersl = gamefolder.getFoldersByName("sl").next();
var ezIter = folderez.getFiles();
var slIter = foldersl.getFiles();
var ss = SpreadsheetApp.getActiveSpreadsheet().getSheetByName(yearfolder+gamefolder);
var i = 2;
while (ezIter.hasNext()) {
var file = ezIter.next();
var name = file.getName(); // use this or the created filename below
var url = file.getUrl();
range = 'AC'+(i);
var filename = 'ezvideo'+(i-1);
var semi = ";";
var hyp = "=hyperlink(\"";
var quot = "\"";
var clos = ")";
var title = hyp + url + quot + semi + quot + name + quot + clos; // str='=hyperlink("' + f.getUrl() + '","' + f.getName() + '")';
ss.getRange(range).setValue(title);
i++;
}
// START OF THE SORTING CODE-Column AC
// get the number of rows of content in Column AC
var ACvals = ss.getRange("AC2:AC").getValues();
var AClast = ACvals.filter(String).length;
// Logger.log("DEBUG: AClast = "+AClast); //DEBUG
// create a range
var range = ss.getRange(2, 29, AClast);
// Logger.log("DEBUG: range = "+range.getA1Notation());//DEBUG
// sort the range by file name
range.sort({
column: 29,
ascending: true
});
// END OF THE SORTING CODE-Column AC
i = 2;
while (slIter.hasNext()) {
var file = slIter.next();
var name = file.getName();
var url = file.getUrl();
range = 'AD'+(i);
var filename = 'slvideo'+(i-1);
var title = hyp + url + quot + semi + quot + file + quot + clos;
ss.getRange(range).setValue(title);
i++;
}
// end of the second WHILE loop.
// START OF THE SORTING CODE-Column AD
// get the number of rows of content in Column AD
var ADvals = ss.getRange("AD2:AD").getValues();
var ADlast = ADvals.filter(String).length;
// Logger.log("DEBUG: ADlast = "+ADlast); //DEBUG
// create a range
var range = ss.getRange(2, 30, ADlast);
// Logger.log("DEBUG: range = "+range.getA1Notation());//DEBUG
// sort the range by file name
range.sort({
column: 30,
ascending: true
});
// END OF THE SORTING CODE-Column AD
}
谢谢大家的帮助!