javascript 如何在单元格中写入进度

axkjgtzd  于 2023-01-08  发布在  Java
关注(0)|答案(1)|浏览(104)

我在Google Apps脚本中创建了一个函数,用于下载和重命名Google Drive中的列表文件。我希望在特定单元格中写入它们的进度。例如,如果第一张图像已下载并在Drive中重命名,则文本应反映在"进度"列中,并带有"已完成"文本。
分别地,将被下载和重命名的图像的数目,"COMPLETED"的文本应该被反映在被称为"PROGRESS"的特定列中它们的单元旁边的列中,因此如果所有的图像已经被下载,则"PROGRESS"列下的所有单元应该在所有列中具有"completed".
请帮助我,因为我已经从字面上尝试了每一件事,但它是没有得到完成。

    • 以下是我想要的图像阐述:**

图像绘制

[谷歌表单网址] https://docs.google.com/spreadsheets/d/1jItuI2tQbpH4A5b9CFS9xWV3mNtnrV2jMRYPxEZSvlM/edit?usp=sharing
[代码]

function onOpen() {
  SpreadsheetApp.getUi() // Or DocumentApp or SlidesApp or FormApp.
      .createMenu('Custom Menu')
      .addItem('Show alert', 'showAlert')
      .addToUi();
}

function showAlert() {
  var ui = SpreadsheetApp.getUi(); // Same variations.

  var result = ui.alert(
     'Please confirm',
     'Are you sure you want to continue?',
      ui.ButtonSet.YES_NO);

  // Process the user's response.
  if (result == ui.Button.YES){
    
    // User clicked "Yes".
    
   {
      let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
let lastRow = sheet.getLastRow();

for (let i = 0; i < lastRow - 1; i++) {
let folder = DriveApp.getFolderById("Folder_id");
let url = sheet.getRange(2 + i, 1).getValue();
let image = SpreadsheetApp.newCellImage().setSourceUrl(url);
let blob = UrlFetchApp.fetch(url).getBlob();
let name = sheet.getRange(2 + i, 2).getValue();
folder.createFile(blob).setName(name);
 SpreadsheetApp.getUi().alert("task has been completed");

    }

  };
  
  } 
    
  else {
    // User clicked "No" or X in the title bar.
    ui.alert('Permission denied.');
  }
}
3bygqnnd

3bygqnnd1#

把确认语句移到循环后的位置,类似这样。

// Process the user's response.
  if (result == ui.Button.YES){
    // User clicked "Yes".
    let sheet = SpreadsheetApp.getActiveSpreadsheet().getActiveSheet();
    let lastRow = sheet.getLastRow();
    for (let i = 0; i < lastRow - 1; i++) {
        let folder = DriveApp.getFolderById("Folder_id");
        let url = sheet.getRange(2 + i, 1).getValue();
        let image = SpreadsheetApp.newCellImage().setSourceUrl(url);
        let blob = UrlFetchApp.fetch(url).getBlob();
        let name = sheet.getRange(2 + i, 2).getValue();
        folder.createFile(blob).setName(name);
        sheet.getRange(2+i,3).setValue("Completed")
        SpreadsheetApp.flush()    
    }
    SpreadsheetApp.getUi().alert("task has been completed")
  } 
  else {
    // User clicked "No" or X in the title bar.
    ui.alert('Permission denied.');
  }

相关问题