2017-03-03 16 views
5

मुझे वेरिएबल के लिए अपरिभाषित प्राप्त हो रहा है: नाम कोई भी मदद क्यों नहीं है कि यह परिणाम प्रदर्शित नहीं कर रहा है। यह लॉगर में प्रदर्शित होगा लेकिन खोज के बाद index.html या वेब पक्ष पर नहीं होगा।वैरिएबल रिटर्न अपरिभाषित

कोड:

// var names =[]; //I tried using a global variable but with no luck 
 

 
function SearchFiles(searchTerm) { 
 
    var searchFor = "title contains '" + searchTerm + "'"; 
 
    var owneris = "and '[email protected]' in Owners"; 
 

 
    var names = []; 
 
    var fileIds = []; 
 
    Logger.log(searchFor + " " + owneris); 
 
    var files = DriveApp.searchFiles(searchFor + " " + owneris); 
 
    while (files.hasNext()) { 
 
    var file = files.next(); 
 
    var fileId = file.getId(); // To get FileId of the file 
 
    fileIds.push(fileId); 
 
    var name = file.getName(); 
 
    names.push(name); 
 
    } 
 

 
    for (var i = 0; i < names.length; i++) { 
 
    //this is showing in the Logger 
 
    Logger.log(names[i]); 
 
    Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]); 
 
    } 
 

 
} 
 

 
function returnNames(names) { 
 
    return '<h3><b>returnNames has ran.!</b></h3> <br>' + names; // Why does this names variable return undefined??? 
 

 
} 
 

 
function doGet(e) { 
 
    var template = HtmlService.createTemplateFromFile('Index'); 
 
    return template.evaluate() 
 
    .setTitle('Search Drive') 
 
    .setSandboxMode(HtmlService.SandboxMode.IFRAME); 
 
} 
 

 

 
function processForm(searchTerm) { 
 
    var resultToReturn; 
 
    Logger.log('processForm was called! ' + searchTerm); 
 
    resultToReturn = SearchFiles(searchTerm); 
 
    Logger.log('resultToReturn: ' + resultToReturn) 
 
    // shows as undefined in the logger 
 
    return resultToReturn; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <base target="_top"> 
 
    <script> 
 
    function displayMessage() { 
 
     var searchTerm; 
 
     searchTerm = document.getElementById('idSrchTerm').value; 
 

 
     console.log('searchTerm: ' + searchTerm); 
 

 
     google.script.run.processForm(searchTerm); 
 
     google.script.run.withSuccessHandler(handleResults).returnNames(); 
 
    } 
 

 

 
    function handleResults(searchTerm) { 
 

 
     console.log('Handle Results was called! '); 
 
     document.writeln(searchTerm); 
 
    } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <input type="text" id="idSrchTerm" name="search"> 
 
    <input type="button" value="submitButton" name="submitButton" onclick="displayMessage()" /> 
 

 
</body> 
 

 
</html>

+1

आपको किस लाइन में त्रुटि मिल रही है? इससे इस मुद्दे को डीबग करने में मदद मिल सकती है। – Rikin

+0

मुझे फ़ंक्शन रिटर्न नाम (नाम) लाइन पर अपरिभाषित हो रहा है- आप देख सकते हैं कि मैंने – OblongMedulla

+2

कई मुद्दों पर टिप्पणी की है। सबसे पहले इंडेक्स कॉल में आप वापस नाम() पर कुछ भी नहीं दे रहे हैं। इस प्रकार जब फ़ंक्शन चलता है तो यह अनिर्धारित हो जाता है और शायद प्रिंट अपरिभाषित होता है। दूसरा, मुझे लगता है कि आप मानते हैं कि नाम वैश्विक रूप से उपलब्ध होंगे जो मैं इसे नहीं देखता हूं। – Rikin

उत्तर

3

मुझे लगता है कि आप गलत तरीके से कर रहे हैं।

Code.gs

function SearchFiles(searchTerm) { 
    var searchFor = "title contains '" + searchTerm + "'"; 
    var owneris = "and '[email protected]' in Owners"; 

    var names = []; 
    var fileIds = []; 
    Logger.log(searchFor + " " + owneris); 
    //Logger.log(searchFor); 
    var files = DriveApp.searchFiles(searchFor + " " + owneris); 
    //var files = DriveApp.searchFiles(searchFor); 
    while (files.hasNext()) { 
    var file = files.next(); 
    var fileId = file.getId(); // To get FileId of the file 
    fileIds.push(fileId); 
    var name = file.getName(); 
    names.push(name); 
    } 

    for (var i = 0; i < names.length; i++) { 
    //this is showing in the Logger 
    Logger.log(names[i]); 
    Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]); 
    } 

    return returnNames(names); // Here call directly returnNames and get the wanted result 
} 

function returnNames(names) { 
    var result = '<h3><b>returnNames has ran.!</b></h3> <br>'; // + names; // Why does this names variable return undefined??? 
    result += '<div>names.length = '+names.length+'</div>'; 

    for(var i=0; i<names.length; i++) { 
    result += '<div>'+names[i]+'</div>'; 
    } 

    return result; 
} 

function doGet(e) { 
    var template = HtmlService.createTemplateFromFile('Index'); 
    return template.evaluate() 
    .setTitle('Search Drive') 
    .setSandboxMode(HtmlService.SandboxMode.IFRAME); 
} 

function processForm(searchTerm) { 
    var resultToReturn; 
    Logger.log('processForm was called! ' + searchTerm); 
    resultToReturn = SearchFiles(searchTerm); 
    Logger.log('resultToReturn: ' + resultToReturn) 
    // shows as undefined in the logger 
    return resultToReturn; 
} 

Index.html

<!DOCTYPE html> 
<html> 

<head> 
    <base target="_top"> 
    <script> 
    function displayMessage() { 
     var searchTerm; 
     searchTerm = document.getElementById('idSrchTerm').value; 

     console.log('searchTerm: ' + searchTerm); 

     //google.script.run.processForm(searchTerm); 
     //google.script.run.withSuccessHandler(handleResults).returnNames(); 
     google.script.run.withSuccessHandler(handleResults).processForm(searchTerm); 
    } 

    function handleResults(searchTerm) { 
     console.log('Handle Results was called! '); 
     document.writeln(searchTerm); 
    } 
    </script> 
</head> 

<body> 
    <input type="text" id="idSrchTerm" name="search"> 
    <input type="button" value="submitButton" name="submitButton" onclick="displayMessage()" /> 
</body> 

</html> 
: यदि आप SearchFiles के अंत में returnNames(names) लौट सकते हैं और आप सिर्फ इस तरह अपने index.html अंदर google.script.run.withSuccessHandler(handleResults).processForm(searchTerm); फोन काम करेंगे

"test" शब्द का उपयोग करके मेरी फ़ाइलों का परिणाम स्क्रीनशॉट:

Screenshot working

+0

यह काम करता है! आपकी सहायता के लिए धन्यवाद!! – OblongMedulla

1

आप इसे अपने गूगल स्क्रिप्ट के नाम के आसपास पारित करने के लिए इस तरह से कोशिश कर सकते हैं।

सर्चफाइल (सर्चटर्म) में आप नाम लौटाते हैं (जो या तो रिक्त सरणी या मूल्य वाले सरणी हो सकते हैं)।

// var names =[]; //I tried using a global variable but with no luck 
 
var Logger = { 
 
    log: function(){ 
 
    console.log(arguments[0]); 
 
    } 
 
}; 
 

 
function SearchFiles(searchTerm) { 
 
    var searchFor = "title contains '" + searchTerm + "'"; 
 
    var owneris = "and '[email protected]' in Owners"; 
 

 
    var names = ["file1","file2","file3"]; 
 
    var fileIds = []; 
 
    Logger.log(searchFor + " " + owneris); 
 
/* var files = DriveApp.searchFiles(searchFor + " " + owneris); 
 
    while (files.hasNext()) { 
 
    var file = files.next(); 
 
    var fileId = file.getId(); // To get FileId of the file 
 
    fileIds.push(fileId); 
 
    var name = file.getName(); 
 
    names.push(name); 
 
    }*/ 
 

 
    for (var i = 0; i < names.length; i++) { 
 
    //this is showing in the Logger 
 
    Logger.log(names[i]); 
 
    Logger.log("https://drive.google.com/uc?export=download&id=" + fileIds[i]); 
 
    } 
 

 
    return names; 
 
} 
 

 
function returnNames(names) { 
 
    return '<h3><b>returnNames has ran.!</b></h3> <br>' + names; // Why does this names variable return undefined??? 
 

 
} 
 

 
function doGet(e) { 
 
    var template = HtmlService.createTemplateFromFile('Index'); 
 
    return template.evaluate() 
 
    .setTitle('Search Drive') 
 
    .setSandboxMode(HtmlService.SandboxMode.IFRAME); 
 
} 
 

 

 
function processForm(searchTerm) { 
 
    var resultToReturn; 
 
    Logger.log('processForm was called! ' + searchTerm); 
 
    resultToReturn = SearchFiles(searchTerm); 
 
    Logger.log('resultToReturn: ' + resultToReturn) 
 
    // shows as undefined in the logger 
 
    return resultToReturn; 
 
}
<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <base target="_top"> 
 
    <script> 
 
    function displayMessage() { 
 
     var searchTerm; 
 
     searchTerm = "DUMMY TEXT";//document.getElementById('idSrchTerm').value; 
 

 
     console.log('searchTerm: ' + searchTerm); 
 

 
     //google.script.run.processForm(searchTerm); 
 
     //google.script.run 
 
     //.withSuccessHandler(handleResults) 
 
     //.returnNames(google.script.run.processForm(searchTerm)); 
 
     processForm(searchTerm); 
 
    } 
 

 

 
    function handleResults(searchTerm) { 
 

 
     console.log('Handle Results was called! '); 
 
     document.writeln(searchTerm); 
 
    } 
 
    </script> 
 
</head> 
 

 
<body> 
 
    <input type="text" id="idSrchTerm" name="search"> 
 
    <input type="button" value="submitButton" name="submitButton" onclick="displayMessage()" /> 
 

 
</body> 
 

 
</html>

+0

मेरे लिए यह वापस लौट रहा है, कोई विचार? – OblongMedulla

+1

मैंने कोड को थोड़ा सा संशोधित करने के लिए संशोधित किया है कि यह चलता है और यह करता है। यदि आप ऊपर पोस्ट किए गए एक ही कोड का पालन कर रहे हैं तो हो सकता है कि आपकी फ़ाइल पढ़ने की प्रक्रिया समस्या का सामना कर रही हो। पता लगाने के लिए वहां डीबगर रखें। – Rikin

+0

यह दिखाता है कि कंसोल लॉग में प्रक्रियाफॉर्म परिभाषित नहीं है। – OblongMedulla

संबंधित मुद्दे