簡體   English   中英

Google Apps Script for Loop 抓取單元格值並插入到公式中

[英]Google Apps Script For Loop To Grab Cell Values and Insert into Formula

我有 5 個可能的位置供用戶輸入:“是、否或任何 URL”。 如果輸入了 URL,我需要將該單元格(URL)的值輸入到單獨工作表中相應單元格的方程中。 這是我所擁有的:

  //Locations of the cells where the user can enter the URL.
  var graphic1_loc = 'A62';
  var graphic2_loc = 'A63';
  var graphic3_loc = 'A64';
  var graphic4_loc = 'A65';
  var graphic5_loc = 'A66';

  //The corresponding locations on the "Briefing" sheet where the images would get inserted.
  var graphic1_placement = 'B45';
  var graphic2_placement = 'B46';
  var graphic3_placement = 'B47';
  var graphic4_placement = 'B48';
  var graphic5_placement = 'B49';

  //main_gen is the name of the sheet where the user enters the data. This just grabs the value
  //the user entered and stores it in the corresponding variable.

  var graphic1 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic1_loc).getValue();
  var graphic2 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic2_loc).getValue();
  var graphic3 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic3_loc).getValue();
  var graphic4 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic4_loc).getValue();
  var graphic5 = SpreadsheetApp.getActiveSheet().getRange('main_gen!'+graphic5_loc).getValue();
  var graphics_placements = ["B45", "B46", "B47", "B48", "B49"]; //These are the corresponding cells where the image would be placed on a separate sheet.

//If any graphic is a URL, insert that image into it's corresponding cell in the Briefing tab.
  for (var num = 0; num < 5; num ++) {
    var graphicformula = '=IMAGE("' + graphic[num] + '",1)';
    if (graphic[num].length > 5) {
   SpreadsheetApp.getActiveSheet().getRange('Briefing!'+graphic[num]_placement).setFormula(graphicformula);
    }
  }

我試圖讓 For 循環中的 If 語句說的是...如果 graphics1 的值的長度 > 5(如果它不是 yes (3) 或 no (2) 然后假設它是一個 URL)然后在“簡報”表中找到相應的單元格以插入公式。 我目前的寫作方式是不正確的,但我想嘗試寫一些東西讓你了解我所追求的東西。 感謝您的任何幫助,您可以提供!

  • 你想運行SpreadsheetApp.getActiveSheet().getRange('Briefing!'+graphic[num]_placement).setFormula(graphicformula); graphic#值為URL時。
  • 例如,當 URL 位於工作表main_gen中的單元格A63中時,您要將=IMAGE(URL,1)的公式放入工作表Briefing的單元格B46

如果我的理解是正確的,這個答案怎么樣? 請將此視為幾種可能的答案之一。

改裝要點:

  • 在您的腳本中,您嘗試使用graphic1, graphic2,,, graphic[num]作為graphic1, graphic2,,,的變量。 在這種情況下, this 不能用作變量。
  • 在您的情況下,工作表main_gen中的單元格A62:A66對應於工作表Briefing的單元格B45:B49 我認為這可用於修改您的腳本。
    • 可以通過ss.getSheetByName("main_gen").getRange("A62:A66").getValues()檢索工作表main_gen單元格A62:A66的值。
  • 為了確認cell的值是否是URL,在這次修改中,我使用了test()
  • 我認為當目標工作表在 for 循環之外聲明時,可以降低流程成本。

當以上幾點反映到你的腳本中時,它變成如下。

修改后的腳本:

var ss = SpreadsheetApp.getActiveSpreadsheet();
var source = ss.getSheetByName("main_gen").getRange("A62:A66").getValues();
var dstSheet = ss.getSheetByName("Briefing");
var graphics_placements = ["B45", "B46", "B47", "B48", "B49"];
for (var num = 0; num < source.length; num ++) {
  if (/https?:\/\//.test(source[num][0])) {
    var graphicformula = '=IMAGE("' + source[num][0] + '",1)';
    dstSheet.getRange(graphics_placements[num]).setFormula(graphicformula);
  }
}

參考:

如果我誤解了您的問題並且這不是您想要的方向,我深表歉意。

暫無
暫無

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

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