简体   繁体   English

当我在javascript文件中运行但未嵌入html文件时,该函数有效

[英]function works when I run it in javascript file but not embedded in html file

I am just starting with this so please bear with me. 我只是从这个开始,所以请忍受我。 Can you please check why function listfiles is not working. 您能否检查一下为什么函数listfiles不起作用。 If I put it in a separate js file and run, logs show the needed output. 如果我将其放在单独的js文件中并运行,日志将显示所需的输出。 But inside html, nothing is displayed. 但是在html内,什么也不显示。

<!DOCTYPE html>
<html>
<body>

<p>
The global function (listfiles) returns the filenmes with certain pattern:
</p>

<p id="demo"></p>

<script>
function listfiles() {

var Extras = "19ibxiuX-xcyNXGs4GBNR_W8zLXSz3mhS";
var filetype = "text/csv";
var filenameformat = /^Report-\w{3}-[A|D]-\d{8}.csv$/;

var fileIterator = DriveApp.getFolderById(Extras).getFiles();

var file;
var filenames = [];
var count = 0;
//Logger.log(count);
filenames[0]="";
//Logger.log(filenames[count]);

while (fileIterator.hasNext()) {
    var file = fileIterator.next();
    //Logger.log(file.getName().search(filenameformat));
    if (file.getName().search(filenameformat) == 0 && file.getMimeType() == filetype) {
    count = count + 1; 
    filenames[count] = file.getName();
    Logger.log(count);
    Logger.log(filenames[count]);
    Logger.log(file.getMimeType());
    }
   };
 return filenames;
}
document.getElementById("demo").innerHTML = listfiles(); 
</script>

</body>
</html>

You script doesn't work because it's not meant to be run like that. 您的脚本不起作用,因为它不是那样运行的。 What you have isn't written in Javascript, but in Google Apps Script. 您拥有的不是用Javascript编写的,而是用Google Apps脚本编写的。 Try running this on script.google.com . 尝试在script.google.com上运行它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM