हेलो दोस्तों आज मैं आपको Memoization के साथ Google Apps Script के परफॉर्मेंस को इम्प्रूव करना सिखाऊंगा। तो चलिए आज के कोड स्निपेट के साथ शुरुआत करते हैं। अलग-अलग समस्याओं को प्राप्त करना पूरी तरह से एक बहुत ही अलग अनुभव देता है। आज मैं आपके साथ जो कोड स्निपेट साझा करने जा रहा हूं, वह है मेमोइज़ेशन के साथ Google Apps स्क्रिप्ट के प्रदर्शन में सुधार कैसे करें।
Google ड्राइव में एक लिफाफे में बहुत सारे CSV दस्तावेज़ होते हैं और CSV रिकॉर्ड में एक विशिष्ट मूल्य का निरीक्षण करने के लिए आपको Google स्क्रिप्ट लिखने की आवश्यकता होती है। व्यवस्था सीधी है:
- Use the Drive API to get a list of CSV files in the specified folder.
- Parse the CSV files one by one using the
Utilities.parseCsv()function. - Read the CSV file, line by line, until the value is found and return the line number.
const findContentInCSVFiles = (folderId, searchString) => {
const folder = DriveApp.getFolderById(folderId);
const files = folder.getFilesByType("text/csv");
while (files.hasNext()) {
const file = files.next();
const fileContent = file.getBlob().getDataAsString();
const linesOfData = Utilities.parseCsv(fileContent, ",");
let found = false;
let lineNumber = 0;
for (; lineNumber < linesOfData.length && !found; lineNumber += 1) {
const line = linesOfData[lineNumber];
found = line.find((element) => element === searchString);
}
if (found) {
return `${searchString} found in line #${
lineNumber + 1
} of file ${file.getName()}`;
}
}
return "String not found :(";
};
आपको हमारे ट्रेंडिंग कोड स्निपेट भी पसंद आ सकते हैं
- How to Improve Performance of Google Apps Script with Memoization
- Google स्लाइड में सभी आकृतियों को समान आकार कैसे बनाएं
- How to Make all Shapes the Same Size in Google Slides
- न्यूनतम बंडल आकार के लिए अपने जावास्क्रिप्ट प्रोजेक्ट्स में लोडाश आयात करने का सबसे अच्छा तरीका
Google स्क्रिप्ट प्रदर्शन का अनुकूलन कैसे करें
सीएसवी रिकॉर्ड को पढ़ने और आवश्यक मूल्य का निरीक्षण करने के लिए कोड बुनियादी है फिर भी उत्पादक नहीं है। आपको प्रत्येक मूल्य के लिए बहुत महंगी गतिविधि खेलनी होगी जिसे आपको CSV दस्तावेज़ों के आयोजक में देखने की आवश्यकता है।
संस्मरण एक बुनियादी सुव्यवस्थित रणनीति है जिसका उपयोग आपके Google Apps स्क्रिप्ट कोड की प्रदर्शनी पर काम करने के लिए किया जा सकता है। आवश्यक विचार यह है कि आप समाप्ति का उपयोग करते हुए एक महंगी क्षमता कॉल के परिणामों को सुरक्षित रखते हैं। यह मानते हुए कि क्षमता को समान तर्कों के साथ फिर से बुलाया जाता है, आरक्षित परिणाम को एक बार फिर क्षमता को कॉल करने और निष्पादित करने के विरोध में वापस कर दिया जाता है।
const memoize = (func) => {
// Cache for storing the previously computed results
const cache = {};
return (...args) => {
// Serializer to convert N arguments to a string
const key = JSON.stringify(args);
if (typeof cache[key] === "undefined") {
cache[key] = func(...args);
}
return cache[key];
};
};
const memoizedFindFunction = memoize(findContentInCSVFiles);
const findContentInFiles = () => {
const FOLDER_ID = "<<folder id>>";
const SEARCH_STRING = "hello world!";
const response = memoizedFindFunction(FOLDER_ID, SEARCH_STRING);
Logger.log(resonse);
};
मेमोइज़ेशन फ़ंक्शन को मूल फ़ंक्शन के तर्कों के साथ बुलाया जाता है। फ़ंक्शन का परिणाम कैश में संग्रहीत किया जाता है और उसी तर्क को फिर से पारित करने पर वापस कर दिया जाता है।

