簡體   English   中英

Google App 腳本在多個工作表中搜索並更新單個單元格值(如果找到)

[英]Google App Script Search and Update Single Cell Value Across Multiple Sheets If Found

如何在多個谷歌工作表(如 20 張工作表)中搜索單元格值,如果找到則更新該值。 比如第17張表(可以是任何一張表,必須是動態的),有一個值需要查找,怎么辦? 多謝你們。

您應該在Sheet上使用 class TextFinder和方法createTextFinder() 另外使用getSheets()獲取給定電子表格的所有工作表。

這里有一個 function,它將搜索一個給定字符串的所有出現並將其替換為另一個給定字符串。

/**
 * Search and replace a string with another string.
 * @param {string} searchTerm term to search for in all sheets
 * @param {string} replaceTerm term to replace all occurences of the search term with
 * @returns number of occurrences replaced
 */
function searchAndReplaceAllSheets(searchTerm, replaceTerm) {
  // get all sheets in your Spreadsheet
  const sheets = SpreadsheetApp.getActiveSpreadsheet().getSheets();

  let totalReplaced = 0;
  // Loop over all sheets
  sheets.forEach((sheet) => {
    // create instance of TextFinder
    const noReplacedElements = sheet
      .createTextFinder(searchTerm) // create text finder
      .matchCase(false) // case-insensitive
      .replaceAllWith(replaceTerm); // replace-all values that contain the search term with the new value
    totalReplaced += noReplacedElements;
  });
  Logger.log(
    `Replaced ${totalReplaced} occurences of ${searchTerm} with ${replaceTerm} in ${sheets.length} sheets`
  );
  return totalReplaced;
}

注意:我沒有對此進行測試,但這應該有效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM